2016-07-11 11 views
1

Ich bin relativ neu zu entwickeln, aber ich habe ein Problem bei der Erstellung eines Plugins für Dynamics CRM festgestellt. Das Plug-in soll das parentcustomerid-Feld auf der Kontakt-Entität null machen, wenn die Account-Entität, mit der es verknüpft ist, aktualisiert wird, wenn das Feld primarycontactid auf etwas anderes aktualisiert wird, entweder null bei Verbindung mit einem anderen Kontakt.CRM-Plugin PreImage nicht verwandte Einheit aktualisieren

Mit dem Code, den ich geschrieben habe zur Zeit keine Fehler ausgelöst und der Code wird ausgeführt, erfolgreich, aber parentcustomerid Feld enthält nach wie vor das Konto es auch verbunden ist, wenn es den Link entfernen soll.

Hier ist der Code i zur Zeit haben:

public void Execute(IServiceProvider serviceProvider) 
    {  
      ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService)); 


      //Obtain the execution context 
      IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); 

      //obtain organizational services 
      IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); 
      IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); 

      EntityReference prePCID; 
      Entity PreImage; 
      // The InputParameters collection contains all the data passed in the message request. 
      if (context.InputParameters.Contains("Target") && 
       context.InputParameters["Target"] is Entity) 
      { 
       // Obtain the image entity from the Pre Entity Images. 
       tracingService.Trace 
        ("trace1: Getting the target entity from Input Parameters."); 
       PreImage = (Entity)context.InputParameters["Target"]; 

       // Verify that the target entity represents an account. 
       // If not, this plug-in was not registered correctly. 
       tracingService.Trace 
        ("trace2: Verifying that the target entity represents a account."); 
       if (PreImage.LogicalName == "account") 
       { 
        if (PreImage.Attributes.Contains("primarycontactid")) 
        { 
         tracingService.Trace 
          ("trace3: Setting the primary contact id in the prePCID."); 
        //prePCID = (EntityReference)PreImage.Attributes["primarycontactid"]; 
        prePCID = (EntityReference)PreImage["primarycontactid"]; 

         tracingService.Trace 
          ("trace4: Primary Contact Name: " + prePCID.Name + " Creating a variable that stores the contact using the prePCID."); 
         Entity contactToRemoveReference = service.Retrieve("contact", prePCID.Id, new ColumnSet("parentcustomerid")) as Entity; 
         tracingService.Trace 
          ("trace5: Removes the id: " + prePCID.Id + " of the parentcustomerid."); 
         contactToRemoveReference.Attributes["parentcustomerid"] = null; 

         service.Update(contactToRemoveReference); 
         tracingService.Trace 
          ("trace6: Execution Successful."); 
        } 
       } 

     } 

}

Wenn jemand für mich dieses Problem lösen könnte, dass großer Dank wäre.

+0

Wie ist Ihr Plugin registriert? –

+0

Die Nachricht lautet Update, die primäre Entität ist account. Wenn ich den Schritt registriert habe, wurde er auf Pre-Op –

+0

gesetzt, das Urbild wird auf Primärkontakt gefiltert, wie es der Schritt ist –

Antwort

0

Sie füllen Entitätsvariable PreImage von der primären Entität in den Eingabeparametern. Ich denke, Sie sollten es von Pre Bild erhalten wie:

Entity PreImage; 
if (context.PreEntityImages.Contains("yourPreImageName") && context.PreEntityImages["yourPreImageName"] != null) 
{ 
    PreImage = context.PreEntityImages["yourPreImageName"]; 
}