Here's the code -- maybe I'm missing something fundamental:
Entity CfP = service.Retrieve("msdyn_contactforparty", salesorder.GetAttributeValue("msdyn_contactperson").Id, new ColumnSet("msdyn_contactid"));
if (null != CfP && CfP.Contains("msdyn_contactid")) {
Entity forUpdate = new Entity("salesorder", salesorder.Id);
forUpdate.Attributes.Add("new_contact", CfP.GetAttributeValue("msdyn_contactid"));
service.Update(forUpdate);
}
This is in a plugin. The problem is, while it works fine at solving the requirement, I am being slathered with system job failures when I don't even expect it to fire, System.NullReferenceException: Object reference not set to an instance of an object.
- I test and step filter on msdyn_contactperson, so that has to be there
- the step is on Update of salesorder, so there has to be a salesorder
- which leaves me questioning CfP
Microsoft doesn't document the result of the failure to find a record to retrieve. Is my test for null wrong? Is it necessary to trap an error on the retrieve? (Of course, how can there be the lookup on the salesorder yet the contactperson not exist!)
Can anyone see where there might be a null object reference?
TIA
PS- sorry for the debugging question