locked
Change Tracking: NULL error when retrieving changes from CRM RRS feed

  • Question

  • All the examples I see for interacting with Change Tracking have a line like this:

    upsertList.AddRange(response.EntityChanges.Changes.Select(x => (x asNewOrUpdatedItem).NewOrUpdatedEntity).ToArray());

    I had this code running just fine and was very happy. However, looking at my service today I can see that it has been spawning errors for the last few days:

    Object reference not set to an instance of an object.

       at MyProject.Dynamics.EntityRepository.<>c.<GetRecordCollection>b__1_1(IChangedItem x) in C:\Projects-Online\MyProject\MyProjectCrmToExternalParty\MyProject.Dynamics\Repository\EntityRepository.cs:line 53
       at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
       at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
       at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
       at MyProject.Dynamics.EntityRepository.GetRecordCollection(IConfigurationModel configurationModel, List`1& upsertList, List`1& deleteList) in C:\Projects-Online\MyProject\MyProjectCrmToExternalParty\MyProject.Dynamics\Repository\EntityRepository.cs:line 53
       at MyProject.Processors.DataProcessor.Process() in C:\Projects-Online\MyProject\MyProjectCrmToExternalParty\MyProject.Processors\DataProcessor.cs:line 57

    There are items in the Changes list (both changes and deletes).

    The code hasn't changed although some Plugins have been added to the CRM instance.

    I just can't see what is actually NULL here. I assume it is something inside the object.

    Can anyone shed some light on the problem?

    I am trying to break out the code into a traditional loop although I am spending a lot of time working out how the objects at boiled down to Entity objects.

    As a simpler secondary question, I had commented out the line which collects removed items because when there aren't any the code broke and I couldn't see how to add a NULL check to the LINQ.

    It was this:

    deleteList.AddRange(response.EntityChanges.Changes.Select(x => (x as RemovedOrDeletedItem).RemovedItem).ToArray());
    

    Didn't want to just wrap the code in a TRY CATCH. Maybe it was a similar issue.

     


    Wednesday, June 28, 2017 8:25 AM

Answers

  • Changed the code th the following and it seemed to solve both problems:

                    upsertList.AddRange(response.EntityChanges.Changes
                        .Where(x => x.GetType().Equals(typeof(NewOrUpdatedItem)))
                        .Select(x => (x as NewOrUpdatedItem).NewOrUpdatedEntity)
                        .ToArray());
     
                    deleteList.AddRange(response.EntityChanges.Changes
                        .Where(x => x.GetType().Equals(typeof(RemovedOrDeletedItem)))
                        .Select(x => (x as RemovedOrDeletedItem).RemovedItem)
                        .ToArray());

    Maybe there is a more elegant solution?  Seems strange that all the code samples out there are broken.  Hope this helps others.

    Wednesday, June 28, 2017 8:50 AM