Godders
Godders

Reputation: 24996

CRM 2011 Post Operation Plugin - Primary entity does not exist when attempting to save a referenced entity

I am attempting to create a plugin for an onsite Dynamics CRM 2011 installation.

I have registered the plugin as follows:

Plugin code as follows:

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") &&
            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 => (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<OrganizationServiceFault> 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;
        }
    }
}

When I profile the plugin using the pluginregistration tool and debug the fault exception I get the following error:

Contact With Id = abbc7e0a-20a0-e111-a36e-005056860004 Does Not Exist.

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.

So I guess my question is, how do you create a referenced entity in a synchronous plugin?

Upvotes: 4

Views: 10700

Answers (1)

John Hoven
John Hoven

Reputation: 4085

Looking at the SDK example for a very similar example - I think you need to register in the sandbox.

Register this plug-in for an account entity, on the Create message, and in asynchronous mode. Alternately, you can register the plug-in on a post-event in the sandbox.

Upvotes: 3

Related Questions