A simple trick to improve performance of Dynamics CRM C# code is to update an entity without retrieving it from the CRM server. This means only one hit to the server takes place instead of two.

update entity record

I have coded an example plugin (below) to demonstrate this. Note this technique can be applied to workflows, console apps and any other code that is interfacing with Dynamics CRM. The plugin sets a contact’s email address to firstname.lastname@work.com when a contact is created.

 

Below is a breakdown/explanation of the code:

First we get the plugin context, organisation service and contact post image for the plugin.

 

We then create the contact to be updated. Note the contact’s ID is set based on the ID from the post image. Email address is set to firstname.lastname@work.com

 

Now we create the Organisation Service Context; attach the contact to be updated, and save changes. Currently, the service context is not tracking the contact to update. Therefore, it has to be attached to the service context.

 

The plugin is set up to execute on creation of a contact as per the below screen shot.

update existing step

 

The plugin has a post image with the contactid, firstname and lastname attribute of the contact as per the below screen shot.

register new image