How to fetch rows based on CRM modifiedon column - dynamics-crm-2011

I need to pull all rows from an entity which were modified recently. I am using the following statement which gives me an error
from e in myEntity
where e.ModifiedOn.HasValue
select e.cust_name
The error states
Invalid 'where' condition. An entity member is invoking an invalid property or method.
Message Invalid 'where' condition. An entity member is invoking an invalid property or method.

Try with:
from e in myEntity
where e.ModifiedOn != null
select e.cust_name

Using Lambda Expressions:
var getModified = myEntity.where(w=>w.ModifiedOn!=null).select(s=>s.cust_name);

Related

how We can count cloud Firestore document of collection, based on document field condition?

I want to count the document of collection based on fields. I found one reference but giving me error
Property 'select' does not exist on type 'CollectionReference.
Below is my firestore service class code. which I applied to fetch the count.
firestore().collection(collectionName).select(documentFieldName).get().then(
(snapshot) => console.log(snapshot.docs.length));
Can any one please suggest there is other way to do ?
Now tried with this pic of code
const a = await firestore().collection(collectionName).where('createdBy',
CONDITIONS.EQUALS, reportType).get().then(a => (a.size));
It giving me as proper response number but added some string text "Invalid status code" I am not sure why added this string
Invalid status code: 19

The binary operator NotEqual is not defined for the types

I am trying to filter some elements like this:
model = model.Where(feature => item
.Input
.Contains(feature
.GetType()
.GetProperty(item.Attribute)
.GetValue(feature)
.ToString()));
item is an object that receives data about the filtering, for instance item.Input is a List<string> containing what the user filled in and item.Attribute (is string) is the column I'm supposed to look at. The field I've tested the error on is a field of type Guid? and it's called AssignedUserId and the curious thing is that this works:
model = model.Where(feature => item.Input.Contains(feature.AssignedUserId.ToString()));
As a note, this works:
model = model.Where(feature => feature
.GetType()
.GetProperty(item.Attribute)
.GetValue(feature)
.ToString() == item.Input.ElementAt(0));
So item.Attribute is populated well, and the filter works.
The error I'm getting is:
System.InvalidOperationException: The binary operator NotEqual is not defined for the types 'Microsoft.EntityFrameworkCore.Storage.ValueBuffer' and 'Microsoft.EntityFrameworkCore.Storage.ValueBuffer'.
What's the problem with getting the field value like in the first code sample?
which version of entityframework are u using?
i got the same error and this is already fixed in 2.1.0-preview1 release like is discussed here issue 9771

Dynamics CRM Plugin - LINQ Query in 'Pre Validation'

I have a Dynamics CRM Plugin registered in "Pre Validation" and trigger on delete.
I have inside it a LINQ query that retrieve the maximum value of a date field of children records for a particular parent record.
Here is my code:
var q = (from e1 in serviceContext.CreateQuery<entity1>()
join e2 in serviceContext.CreateQuery<entity2>() on e1.typeid.Id equals e2.codeId
where e1.statecode == 0 && e1.ParentId.Id.Equals(new Guid(ParentGuidStr))
orderby e1.dt descending
select new {e1.dt, e2.code}).ToList();
I am getting the following error on the above query When the record that the plugin triggers on is INACTIVE:
PreValidateEntity1Delete PlugIn Error:
System.Reflection.TargetInvocationException: Exception has been thrown
by the target of an invocation. ---> System.ArgumentNullException:
Value cannot be null. Parameter name: g
I am NOT sure why I am getting the above error and if the link between an inactive children record and a parent record got broken in a LINQ query or there is another reason.
First, please ensure all children records have reference to parent record.
Then, change your where contidion into:
where e1.statecode == 0 && e1.ParentId != null && e1.ParentId.Id.Equals(new Guid(ParentGuidStr))
The problem is that an EntityReference field (e1.ParentId) may be null and when you tried access a null entity, the error appears.
Then because of that, you should also make sure e1.typeid is not null in the join condition too.
Or, you can try a workaround with 2 separate queries, and collect information from their results.
Good luck!

Saving to a Entity that has Many To Many relationship

I have a set of tables (ConditionTemplate and KeyWord) that have a many to many relationship. In code I am trying to add a Keyword to a specific ConditionTemplate record. Unfortunately, when I think I'm adding a Keyword to a specific condition I'm getting an error as if it's adding a new Keyword without being associated to a condition.
An Image of my Model:
My Code:
Global Variables Creation:
EnterpriseEntities EE;
ConditionTemplate myConditionTemplate;
Load Global Variables:
EE = new EnterpriseEntities();
EE.Database.Connection.ConnectionString = Myapp.EnterpriseEntityConnectionString;
myConditionTemplate = EE.ConditionTemplates.Where(c => c.TemplateCode == "17D").FirstOrDefault();
The above code loads a single Condition with Many Keywords.
Available Keywords are in a listbox and the user pushed a button to select a keyword(s) to move to the condition. This is the code that handles that.
foreach (KeyWord SelectedKeyWord in ListBoxAvailableKeyWords.SelectedItems)
{
KeyWord NewKeyWord = new KeyWord
{
KeyWordID = SelectedKeyWord.KeyWordID,
ID = SelectedKeyWord.ID,
Word = SelectedKeyWord.Word
};
myConditionTemplate.KeyWords.Add(NewKeyWord);
}
Then the user pushes a button to save changes and I call
EE.SaveChanges
Then I get this error:
System.Data.UpdateException: An error occurred while updating the
entries. See the inner exception for details. --->
System.Data.SqlClient.SqlException: Violation of UNIQUE KEY constraint
'IX_KeyWord'. Cannot insert duplicate key in object 'dbo.KeyWord'. The
duplicate key value is (ADJUDICATION). The statement has been
terminated.
If I remove the code that sets the word property (Word = SelectedKeyWord.Word
) when I create the keyword object I get this error.
System.Data.Entity.Validation.DbEntityValidationException: Validation
failed for one or more entities. See 'EntityValidationErrors' property for more details.
Which tells me the word field is required.
In order to tell EF that the KeyWords you selected already exist in the database and to avoid the problem you must attach them to the context:
foreach (KeyWord SelectedKeyWord in ListBoxAvailableKeyWords.SelectedItems)
{
KeyWord NewKeyWord = new KeyWord
{
// You actually only need to set the primary key property here
ID = SelectedKeyWord.ID
};
EE.KeyWords.Attach(NewKeyWord);
myConditionTemplate.KeyWords.Add(NewKeyWord);
}
Edit
If the KeyWord entities are already attached to your context (because they have been loaded before with the same context for example) you can use instead:
foreach (KeyWord SelectedKeyWord in ListBoxAvailableKeyWords.SelectedItems)
{
KeyWord NewKeyWord = EE.KeyWords.Find(SelectedKeyWord.ID);
myConditionTemplate.KeyWords.Add(NewKeyWord);
}

(SPFieldLookupValue) splistitem of Lookup type throws Object reference not set to an instance of an object exception

I have a sharepoint list which has some Lookup fields. When I iterate through the items in code, I get the following error:
Object reference not set to an instance of an object.
This error appears only on lookup fields when they are not filled in with any value. I tried to use SPFieldLookupValue to check for null values, but I still get the error.
This is how I check for null values:
SPFieldLookupValue value = new SPFieldLookupValue(listItem[columnDisplayName].ToString());
if (value.LookupValue != null)
Any help guys?
The reason why you get this exception lies here: listItem[columnDisplayName].ToString() because listItem[columnDisplayName] have no value and returns null you trying to call ToString() on null object so it throws "Object reference not set to an instance of an object exception".
If you simply want to check if item field is not null then do like that:
if (listItem[columnDisplayName]!=null)
{
//here you can access listItem[columnDisplayName] safely
}

Resources