Development Desk

Just another WordPress.com site

Monthly Archives: April 2015

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