Condividi tramite


Utilizzare la classe Entity per aggiungere o aggiornare le associazioni tra record correlati

 

Data di pubblicazione: gennaio 2017

Si applica a: Dynamics 365 (online), Dynamics 365 (on-premises), Dynamics CRM 2016, Dynamics CRM Online

In Microsoft Dynamics 365 (online e locale), puoi usare i metodi IOrganizationService.Associate e IOrganizationService.Disassociate per creare e rimuovere le associazioni tra record correlati.

Per creare associazione, è innanzitutto necessario stabilire l'ID univoco dell'entità di destinazione da associare. Quindi sarà necessario creare una raccolta di entità da associare all'entità di destinazione. Successivamente, è necessario definire la relazione tra entità nella raccolta e l'entità di destinazione. Infine, si passeranno queste informazioni al metodo Associate. Le stesse informazioni vengono comunicate al metodo Disassociate quando si rimuove un'associazione.

Nell'esempio di codice seguente viene illustrato come creare le associazioni tra record correlati e annullarle.

// The account ID would typically be passed in as an argument or determined by a query.
// The contact ID would typically be passed in as an argument or determined by a query.
// Associate the accounts to the contact record. 
//Create a collection of the entity ids that will be associated to the contact.
EntityReferenceCollection relatedEntities = new EntityReferenceCollection();
relatedEntities.Add(new EntityReference("account", _account1Id));
relatedEntities.Add(new EntityReference("account", _account2Id));
relatedEntities.Add(new EntityReference("account", _account3Id)); 
// Create an object that defines the relationship between the contact and account.
Relationship relationship = new Relationship("account_primary_contact");
  //Associate the contact with the 3 accounts.
_orgService.Associate("contact", _contactId, relationship, relatedEntities); 
Console.WriteLine("The entities have been associated."); 
//Disassociate the records.
_orgService.Disassociate("contact", _contactId, relationship, relatedEntities); 
Console.WriteLine("The entities have been disassociated.");

Vedere anche

Utilizzare la classe di entità con associazione tardiva nel codice
Utilizzare la classe Entity per creare, aggiornare ed eliminare
Comportamento della relazione di entità
Esempio: creazione, recupero, aggiornamento ed eliminazione (associazione ritardata)

Microsoft Dynamics 365

© 2017 Microsoft. Tutti i diritti sono riservati. Copyright