Note that there are some explanatory texts on larger screens.

plurals
  1. POCRM 2011 Post Operation Plugin - Primary entity does not exist when attempting to save a referenced entity
    text
    copied!<p>I am attempting to create a plugin for an onsite Dynamics CRM 2011 installation.</p> <p>I have registered the plugin as follows:</p> <ul> <li>Message: Create</li> <li>Primary entity: Contact</li> <li>Stage of execution: Post-operation</li> <li>Execution mode: Synchronous</li> <li>Execution order: 1</li> </ul> <p>Plugin code as follows:</p> <pre class="lang-cs prettyprint-override"><code>public void Execute(IServiceProvider serviceProvider) { var pluginExecContext = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); var orgServiceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); var orgService = orgServiceFactory.CreateOrganizationService(pluginExecContext.UserId); var orgServiceContext = new OrganizationServiceContext(orgService); var tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService)); if (pluginExecContext.InputParameters.Contains("Target") &amp;&amp; pluginExecContext.InputParameters["Target"] is Entity) { var target = (Entity)pluginExecContext.InputParameters["Target"]; if (target.LogicalName != Contact.EntityLogicalName) return; try { var customerServicesUser = orgServiceContext.CreateQuery(SystemUser.EntityLogicalName) .Where(x =&gt; (string)x["fullname"] == "Customer Services").FirstOrDefault(); if (customerServicesUser == null) throw new InvalidPluginExecutionException("No Customer Services user exists."); var sendEmail = new cdi_sendemail { cdi_fromrecordowner = false, cdi_contactid = new EntityReference(Contact.EntityLogicalName, pluginExecContext.PrimaryEntityId), cdi_fromid = new EntityReference(SystemUser.EntityLogicalName, customerServicesUser.Id) }; tracingService.Trace("PostContactCreate plug-in: Creating the cdi_sendemail entity."); orgService.Create(sendEmail); } catch (FaultException&lt;OrganizationServiceFault&gt; ex) { throw new InvalidPluginExecutionException("An error occurred in the PostContactCreate plug-in.", ex); } catch (Exception ex) { tracingService.Trace("PostContactCreate plug-in: {0}", ex.ToString()); throw; } } } </code></pre> <p>When I profile the plugin using the pluginregistration tool and debug the fault exception I get the following error:</p> <blockquote> <p>Contact With Id = abbc7e0a-20a0-e111-a36e-005056860004 Does Not Exist.</p> </blockquote> <p>Which I sort of understand as the plugin is executing within a SQL transaction that has yet to commit. The "FollowupPlugin" within the CRM SDK samples, which also creates a referenced entity, states that it needs to be registered asynchronously, which would also make sense as that allows the SQL transaction to commit.</p> <p>So I guess my question is, how do you create a referenced entity in a synchronous plugin?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload