Development Desk

Just another WordPress.com site

Associate Many to Many Entity Record in CRM 2013

when there is many to many or n to n (n:n) relationship is created between two entities, CRM creates new table as connection or a bridge between these entities to store id’s of both entities. Suppose there is a student and he has been studied in different institutes and there could be n:n relationship between them lets say

there relationship name is (crm database table name) crm_student_institute

target entity could be student

linked entity could be institute so

//string strEntityRelationshipName = crm_student_institute
//CrmServiceProvider serviceProvider is new crm 2013 service provider which contains IOrganisationService object inside of it serviceProvider.Service
//Entity targetEntity = Student Entity
//Entity linkedEntity = institute entity
public static bool AssociateManyToManyEntityRecords(CrmServiceProvider serviceProvider, Entity targetEntity, Entity linkedEntity, string strEntityRelationshipName)
{
    AssociateRequest request = new AssociateRequest();
    EntityReference targetEntityReference = new EntityReference(targetEntity.LogicalName, targetEntity.Id);
    EntityReference linkedEntityReference = new EntityReference(linkedEntity.LogicalName, linkedEntity.Id);
    request.Target = targetEntityReference;
    request.RelatedEntities = new EntityReferenceCollection { linkedEntityReference };
    request.Relationship = new Relationship(strEntityRelationshipName);

    serviceProvider.Service.Execute(request);

    return true;
}
Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: