Plugin Pre Operation Create - Update field error

Nick picture Nick · Feb 11, 2014 · Viewed 13.8k times · Source

My plugin fire on Pre Create operation on Entity X. When trying to update a field on the Entity X using the following code I am getting error:

trEntity = (Entity)context.InputParameters["Target"];
trGuid = (Guid)trEntity.Id;

tr = (Entity)service.Retrieve("EntityX", trGuid,
                    new ColumnSet(new string[] { "field_a", "field_b" }));


tr["field_a"] = null;
service.Update(tr);

The error I am getting is: Entity X with Id = 11505683-2292-b537-e311-143710e56fb7 Does Not Exist

Answer

Josh Painter picture Josh Painter · Feb 11, 2014

Since you are in Pre-Create, the entity doesn't exist yet in the database.

You don't need to explicitly call Update in a Pre event. You can just update the Target entity (trEntity in your case) and the changes you make will be saved with the Create operation. The Target entity is the actual entity that is about to be created, so feel free to update fields directly on the Target in the Pre event.

trEntity = (Entity)context.InputParameters["Target"];
trEntity["field_a"] = null;