EntityState must be set to null, Created or Changed CRM error

Hello everyone,

If you are working with an update plugin and got this “EntityState must be set to null, Created (for Create message) or Changed (for Update message) when attempting to update an entity” error you are in the right place to solve this problem.

Just like me probably you might have used Update method of CrmService class to update your record while you are still processing the update operation.

Here is how it looks:

service.Update(contact);

Instead of this usage you should use UpdateObject method of your OrganizationServiceContext like this:

context.UpdateObject(object);
context.SaveChanges();

Here comes an important situation. To Avoid Multiple Trigger Of Same Update Plugin you should use a codeline like below at the beginning of your Execute method of your update plugin:

if (localContext.PluginExecutionContext.Depth > 1)
{
   return;
}

Hope this helps!

See you 🙂

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *