Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM forum
Suggested answer

Value cannot be null. Parameter name: item

Posted on by 10

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),
                                        }
                                    }
                                }
                            },
                        }
                    );



LogTrace:
pastedimage1678454447328v1.png

Any inputs will be highly appreciated.

  • Suggested answer
    Guido Preite Profile Picture
    Guido Preite 54,063 Moderator on at
    RE: Value cannot be null. Parameter name: item

    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

  • RE: Value cannot be null. Parameter name: item

    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.',

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,321 Most Valuable Professional on at
    RE: Value cannot be null. Parameter name: item

    Then I would recommend profiling the execution of your plugin using Plugin profiler, replay and see what's wrong with the query.

    learn.microsoft.com/.../tutorial-debug-plug-in

  • RE: Value cannot be null. Parameter name: item

    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 { .... } );
    }

  • a33ik Profile Picture
    a33ik 84,321 Most Valuable Professional on at
    RE: Value cannot be null. Parameter name: item

    Hello,

    Can you please provide the code where you instantiate/set the variable with the name contactId?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,532 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,501 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans