Associate records in plugin

When creating N:N relationships between entities, CRM allow us to associate records. This is a great functionality to track and reference records through entities. Although this can be done manually, find below a method to associate two records programmatically:

///
/// Associate two records
/// 
public void Associate_Records(EntityReference Main_Record, string Relationship_Name, EntityReference AssociatedRecord, IOrganizationService Service)
{
   Service.Associate(Main_Record.LogicalName,
                     Main_Record.Id,
                     new Relationship(Relationship_Name),
                     new EntityReferenceCollection()
                     {
                        AssociatedRecord
                     });
}

 

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