LameCoder
LameCoder

Reputation: 1297

Plugin Create Pre-Operation add related entity

So when an entity is created, I want to search for some other entities and relate them. However I get a not-exists error if I update the other entities with the entity ref of the newly created one, and that makes sense because it's PRE create.

So I updated it like so thinking this would do the trick, but I'm getting an "unknown error" from somewhere deep in the heart of CRM outside of my control:

var newEntity = (Entity)localContext.PluginExecutionContext.InputParameters["Target"];
var allUnits = localContext.OrganizationService.RetrieveMultiple(....); //grabs the units that will be used

newEntity.RelatedEntities.Add(new Relationship("ntcp_equipment_unit"), new EntityCollection(allUnits.Entities));

Result:

System.ServiceModel.FaultException`1 occurred
Message: A first chance exception of type 'System.ServiceModel.FaultException`1' occurred in Microsoft.Crm.Extensibility.dll
Additional information: An unexpected error occurred.

Upvotes: 1

Views: 2020

Answers (1)

Daryl
Daryl

Reputation: 18895

I'm not sure what your error is, but I would change the plugin to be Create, Post-Operation. It's still within the database transaction, but now the Target will exist so attempting to add the relationship will succeed rather than fail.

Upvotes: 2

Related Questions