Plugin Pre Operation Create - Update field error
Asked Answered
F

2

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

Fibster answered 11/2, 2014 at 0:23 Comment(0)
P
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;
Ptomaine answered 11/2, 2014 at 1:15 Comment(7)
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.Fibster
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.Fibster
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;Fibster
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.Ptomaine
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;Fibster
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.Ptomaine
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.Ptomaine
S
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.

Sebastien answered 11/2, 2014 at 1:22 Comment(1)
I am using Developer toolkit and creating the service as follows: IPluginExecutionContext context = localContext.PluginExecutionContext; IOrganizationService service = localContext.OrganizationService;**Fibster

© 2022 - 2024 — McMap. All rights reserved.