Associate and Disassociate Records using C# Code – Dynamics CRM/365

Hi Everyone,

This might be a old one but somehow I could not get this piece code very easily.

Here is the piece of C# code for Associating and Dissociating two records with N:N relationship.

Associate
string relationshipName = “<>”;
Relationship relationship = new Relationship(relationshipName);
EntityReferenceCollection relatedEntities = newEntityReferenceCollection();
EntityReference secondaryEntity = new EntityReference(“SecondEntitySchemaName”, GUIDOfSecondEntityRecord);
relatedEntities.Add(secondaryEntity);
crmService.Associate(“FirstEntitySchemaName”, GUIDOfFirstEntityRecord, relationship, relatedEntities);

Disassociate
string relationshipName = “<>”;
Relationship relationship = new Relationship(relationshipName);
EntityReferenceCollection relatedEntities = newEntityReferenceCollection();
EntityReference secondaryEntity = new EntityReference(“SecondEntitySchemaName”, GUIDOfSecondEntityRecord);
relatedEntities.Add(secondaryEntity);
crmService. Disassociate (“FirstEntitySchemaName”, GUIDOfFirstEntityRecord, relationship, relatedEntities);


Hope this helps.

Happy 365’ing

Gopinath

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s