Reputation: 569
I'm trying to associate related records of an entity to a newly created one. The pluggin triggers on creation and pre-operation.
The error occurs when trying to associate the collection to the new entity : "new_ligneContrat With Id = ad630ba6-684e-e111-92e3-00155d151905 Does Not Exist"
Here is my code :
public void Execute(IServiceProvider serviceProvider)
{
// Instanciation des services
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(null);
Entity target = (Entity)context.InputParameters["Target"];
EntityReference contrats = (EntityReference)target.Attributes["new_contratsid"];
FetchExpression fetch = new FetchExpression(@"
<fetch distinct='false' mapping='logical'>
<entity name='" + context.PrimaryEntityName + "'><link-entity name='new_contrats' alias='nombreligne' from='new_contratsid' to='new_contratsid'><filter type='and'><condition attribute='new_contratsid' value='" + contrats.Id + "' operator='eq'></condition></filter></link-entity></entity></fetch>");
EntityCollection lines = service.RetrieveMultiple(fetch);
// Vérification qu'il y a au moins une ligne de contrat associée
if (lines.Entities.Any())
{
var first = lines.Entities.Last();
if (first.GetAttributeValue<OptionSetValue>("statecode").Value == 1)
{
FetchExpression query = new FetchExpression(@"
<fetch distinct='false' mapping='logical'>
<entity name='incident'><filter type='and'><condition attribute='new_lignecontrat' value='"+first.Id+"' operator='eq'/></filter></entity></fetch>");
EntityCollection incident = service.RetrieveMultiple(query);
if (incident.Entities.Any())
{
foreach (var e in incident.Entities)
{
e.Attributes["new_lignecontrat"] = new EntityReference (target.LogicalName, target.Id);
}
}
}
}
What is wrong???
Thanks in advance!!
Edit 1: ok seems logical since the record does not exist yet ><. Just one thing : How can I change the value of a lookup field? What is its type?
Edit 2: I've got no error when executing my code, but fields of the incident entity do not update ><'....I've tested my code with invalidPluginExceptions and the end of the code is reached...Here is the code :
Edit 3 : Code updated...
Upvotes: 2
Views: 998
Reputation: 5362
To answer your original question and its edit, yeah, you can't associate the record with another record when the core database operation hasn't been done yet.
Pre-operation: Stage in the pipeline for plug-ins that are to execute before the main system operation. Plug-ins registered in this stage are executed within the database transaction.
So to handle association, you can either change the stage to post-operation, or have one IPlugin
class handle the pre-operation stage and another handle the post-operation stage, either in one or multiple projects.
To answer the edit, lookup fields are of the class EntityReference
. (Looks like you're working with a 1:N relationship?)
To answer the second edit, I don't see anywhere in your code snippet where you assign the new EntityReference
to your target Entity
. Moreover, you don't have to issue an Update
request to the service in the pre-operation stage because the core database operation has not been performed yet. You can just set the attribute of the Entity
to be equal to the the value of your choice, and this change will be carried over to the database.
if (entity.Attributes.ContainsKey("new_lignecontrat"))
{
entity.Attributes["new_lignecontrat"] = YourEntityReference;
}
else //attribute not included in the plugin operation
{
entity.Attributes.Add("new_lignecontrat", YourEntityReference);
}
Microsoft has a demonstration of this concept in the SDK:
\sdk\samplecode\cs\plug-ins\accountnumberplugin.cs
Upvotes: 3