0
votes

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.

1

1 Answers

2
votes

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!