Today I am going to write about a common mistake that CRM developers do very often. If you don’t correct this right now you will face some serious issues with your CRM system and some unpredictable behaviours even in Production systems which will be very hard to troubleshoot.
What is it?
Most of the time you will need to update a CRM record in your plugins or custom workflow activities or some of your console applications which you might write for various requirements.
I will take a plugin for example; Look at this basic plugin which updates the Do Not Email field of a contact record.
public class UpdateContact:IPlugin
public void Execute(IServiceProvider serviceProvider)
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.InitiatingUserId);
using (XrmServiceContext serviceContext = new XrmServiceContext(service))
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
Entity entity = (Entity)context.InputParameters["Target"];
if (entity.LogicalName == Contact.EntityLogicalName)
var contactRecord = serviceContext.ContactSet.FirstOrDefault(c => c.Id == entity.Id);
//wrong way! This will update all the fields of the record.
contactRecord.DoNotEMail = false;
//Correct way to update. This will update only the specified field(s)
Contact contactRecordToUpdate = new Contact();
contactRecordToUpdate.Id = contactRecord.Id;
contactRecordToUpdate.DoNotEMail = false;
Correct way is get the ID of the entity from the input parameters; then create a new instance of the entity and set the ID and the field values. Then pass that instance when you call the update method.