CRM 2011 I / O Plugin - Primary object does not exist when trying to save a reference object

I am trying to create a plugin for in-place installation of Dynamics CRM 2011.

I registered the plugin as follows:

  • Message: Create
  • Primary organization: Contact
  • Stage of execution: post-operation
  • Run Mode: Synchronous
  • Execution order: 1

Plugin 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") && 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 a plugin using the plugin registration tool and debug the error, I get the following error:

Contact with Id = abbc7e0a-20a0-e111-a36e-005056860004 does not exist.

Which view do I understand since the plugin is executed in an SQL transaction that has not yet been executed. The “FollowupPlugin” in the CRM SDK samples, which also creates a reference object, indicates that it should be registered asynchronously, which also makes sense, as this allows SQL transactions to complete transactions.

So, I think my question is how do you create an object reference in a synchronous plugin?

+4
source share
1 answer

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

Register this plugin for the account object in the "Create" message and in asynchronous mode. Alternatively, you can register the plug-in in post-event in the sandbox .

+3
source

Source: https://habr.com/ru/post/1413002/


All Articles