5

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

Mifeet
  • 12,949
  • 5
  • 60
  • 108
Nick
  • 780
  • 2
  • 13
  • 33

2 Answers2

13

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;
Josh Painter
  • 4,071
  • 21
  • 26
  • Thanks Josh. So you think because it is Pre Create and I am using the service to uodate I am getting the error? I will try using `trEntity["field_a"] = null;` instead. – Nick Feb 11 '14 at 03:05
  • It worked fine. I have one more issue which is I need to get the value of an Attribute which is an EntityReference. When I use `var tType = (EntityReference)trEntity.Attributes["trTypeId"]; var trType = tType.Name;` I am NOT getting the value and always get null. Please advice. – Nick Feb 11 '14 at 14:58
  • I can get the id of the EntityReference but NOT the Name. So the following code is working fine: `var tType = (EntityReference)trEntity.Attributes["trTypeId"]; var trType = tType.Id;` – Nick Feb 11 '14 at 18:15
  • Lookups and EntityReferences are always fun. Sometimes the name is there and sometimes it isn't. If you look in the database, the name of the lookup is stored directly with the parent record sometimes (customer and regarding lookup fields) but usually it is not. If you need the name, the safest way is to use the service to retrieve that related entity's name attribute. – Josh Painter Feb 11 '14 at 18:56
  • With pre Create Operation when using service I am getting error. So the following code will NOT work: `tr = (Entity)service.Retrieve("EntityX", trGuid, new ColumnSet(new string[] { "trTypeId" })); tType = (EntityReference)tr.Attributes["trTypeId"]; trType = tType.Name;` – Nick Feb 11 '14 at 19:13
  • Don't use `trTypeId` in your ColumnSet - use the schema name from CRM, probably `[entityname]id`. Also don't forget to include the `name` column in your `ColumnSet`. Then `tr` will have both the id and name column - it won't be an `EntityReference`, it will come back from the service as a normal `Entity`. – Josh Painter Feb 11 '14 at 19:25
  • And `EntityX` needs to be the name of the lookup entity - not the current one you are updating. You are basically retrieving that other entity so you can get the name off of it. – Josh Painter Feb 11 '14 at 19:27
0

How are you creating your service? This also happens when you try to update a record outside of the current transaction i.e. using a manually created OrganizationServiceProxy instead of using the one provided by IOrganizationServiceFactory.CreateOrganizationService.

Adi Katz
  • 548
  • 3
  • 9
  • I am using Developer toolkit and creating the service as follows: `IPluginExecutionContext context = localContext.PluginExecutionContext; IOrganizationService service = localContext.OrganizationService;**` – Nick Feb 11 '14 at 02:59