Announcements
Hi All,
We are using Dynamics CRM and we try to retrieve values with the query.
Time to time we are receiving an error message like "Value cannot be null. Parameter name: item".
What we noticed is that this error occur either in context.OrganizationService.RetrieveMultiple(...) or context.OrganizationService.Retrieve(...) methods.
This methods are part of Microsoft.Xrm.Sdk.IOrganizationService:
Example of the code where the error occur:
EntityCollection results = context.OrganizationService.RetrieveMultiple( new QueryExpression { EntityName = Contact.EntityLogicalName, ColumnSet = new ColumnSet(Contact.Fields.CustomField), Criteria = { Filters = { new FilterExpression { FilterOperator = LogicalOperator.And, Conditions = { new ConditionExpression(Contact.Fields.ContactId, ConditionOperator.Equal, contactId), } } } }, } );
that code can be changed with a Retrieve because you are retrieving a single contact by the id, however try with this simpler version of the code and check if you still get the same error or not:
QueryExpression queryContact = new QueryExpression("contact"); queryContact.ColumnSet = new ColumnSet("contactid"); // add your custom field here queryContact.Criteria.AddCondition("contactid", ConditionOperator.Equal, contactId); EntityCollection results = context.OrganizationService.RetrieveMultiple(queryContact);
if it works it means something in your variables is not correct
the single retrieve version would be:
Entity contact = context.OrganizationService.Retrieve("contact", contactId, new ColumnSet("contactid")); // add your custom field inside ColumnSet
The problem that we cannot simulate the same issue on our side.
The query is correct, we have tried to debug it and use logging to see what kind value was send in RetrieveMultiple() method. Everything looks okay.
The error occur on Microsoft.XRM.SDK side. What is trigger it is unclear.
This error is not reproducible locally and not consistent.
It is somewhat similar or related to the error 'Collection was modified; enumeration operation may not execute.'
The 2 errors coming in different time from the same part of the code related to get value with query using RetrieveMultiple() method
1) Type: 'System.ArgumentNullException',
Message: 'Value cannot be null. Parameter name: item',
2) Type: 'System.InvalidOperationException',
Message: 'Collection was modified; enumeration operation may not execute.',
Then I would recommend profiling the execution of your plugin using Plugin profiler, replay and see what's wrong with the query.
The contactId is GUID with the id of the contact. We set this value in this variable above.
We have also a check for null and empty value before we start with RetrieveMultiple.
if (contactId != null && contactId != Guid.Empty) { EntityCollection results = context.OrganizationService.RetrieveMultiple( new QueryExpression { .... } ); }
Hello,
Can you please provide the code where you instantiate/set the variable with the name contactId?
André Arnaud de Cal... 291,359 Super User 2024 Season 2
Martin Dráb 230,370 Most Valuable Professional
nmaenpaa 101,156