0
votes

I got this piece of code in my account plugin, which was working before, but keep having this wired issue now.

What it tries to do is, when the account is updated, also update the primary contact.

ColumnSet contactCols = new ColumnSet(new string[] { "firstname"});
Entity contact = orgService.Retrieve("contact", contactId, contactCols);
tracer.Trace("firstname is " + contact["firstname"]);

contact["firstname"] = DateTime.Now.ToString();
orgService.Update(contact);  

The Retrieve() works, but the Update() will throw the following exception:

Unhandled Exception: 
Microsoft.Xrm.Sdk.InvalidPluginExecutionException: 
System.ServiceModel.FaultException`1[Microsoft.Xrm.Sdk.OrganizationServiceFault]: 
System.ServiceModel.FaultException`1[Microsoft.Xrm.Sdk.OrganizationServiceFault]: 
System.NullReferenceException: 
Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #BF42D86C (Fault Detail is equal to Microsoft.Xrm.Sdk.OrganizationServiceFault). (Fault Detail is equal to Microsoft.Xrm.Sdk.OrganizationServiceFault).
  at CRMSyncPlugin.SyncEntityClass.Execute(IServiceProvider serviceProvider)
  at Microsoft.Crm.Asynchronous.V5ProxyPlugin.Execute(IServiceProvider serviceProvider)
  at Microsoft.Crm.Asynchronous.EventOperation.InvokePlugin(AsyncExecutionContext context, IPlugin pluginInstance)

It said NullReferenceException, I just couldn't figure out what is null.

==========================================================

After trying what @Nicknow suggested, still the same error. Here is what I got from the trace:

Retrieving Contact: 048f9564-81b4-e311-a27c-0026553e0f7c
Retrieved Contact
firstname is John

The retrieve worked, just the update failed. Thx

3
First, are you sure that the contact record being returned as a value for firstname? If it doesn't you will get an exception. Is tracer.Trace("firstname is " + contact["firstname"]); getting written the trace? Can you post all of the ErrorLog file? Can you hook up the Visual Studio Debugger and catch the exception? That would tell you the line and null reference.Nicknow
Please show full text of your exception. Are you receiving trace log?Siddique Mahsud
Hi @Siddique Mahsud, above is the full text of exception, yes I received the trace log, firstname is successfully received, just the update failed. ThxVincent Zhong

3 Answers

1
votes

I've added some additional tracing and null checking, this should work:

tracer.trace("Retrieving Contact: {0}", contactId.ToString());
ColumnSet contactCols = new ColumnSet(new string[] { "firstname"});
Entity contact = orgService.Retrieve("contact", contactId, contactCols);
tracer.trace("Retrieved Contact");

tracer.Trace("firstname is " + contact.Attributes.Contains("firstname") ? contact["firstname"] : "(No Value for firstname)");

if (contact.Attributes.Contains("firstname")) contact["firstname"] = DateTime.Now.ToString();
else contact.Attributes.Add("firstname", DateTime.Now.ToString());
orgService.Update(contact);  
0
votes

You might check to see if there is another plugin that runs on the Update of Contact - the error could be coming from that other plugin instead of this one.

0
votes

I generally don't try to update the object that I got from the Retrieve call. Just new up a Contact, set the ID, and include only the fields you're looking to change.

ColumnSet contactCols = new ColumnSet(new string[] { "firstname"});
Entity contact = orgService.Retrieve("contact", contactId, contactCols);
tracer.Trace("firstname is " + contact["firstname"]);
var updateContact = new Contact();
updateContact["contactId"] = contactId;
updateContact["firstname"] = DateTime.Now.ToString();
orgService.Update(updateContact); 

I've found this tends to avoid these sorts of issues.