Associate and Disassociate Many to Many relationship records using C# in Microsoft Dynamics CRM 2011

Below code used to associate and disassociate records for Many to Many relationship through C#.

For ex: If there we have created Many to Many relationship between Contact and Account. The association of records will be like below.

                   
                    AssociateRequest request = new AssociateRequest();
                    EntityReference moniker1 = new EntityReference("contact", new Guid(contactGuid));
                    EntityReference moniker2 = new EntityReference("account", new Guid(businessGuid));
                    EntityReferenceCollection relatedEntities = new EntityReferenceCollection();
                    relatedEntities.Add(moniker1);
                    request.Target = moniker2;
                    request.RelatedEntities = new EntityReferenceCollection { moniker1 };
                    request.Relationship = new Relationship("crisp_account_contact");
                    service.Associate(moniker2.LogicalName, moniker2.Id, request.Relationship, relatedEntities);

To Disassociate the record:

            AssociateRequest request = new AssociateRequest();
            EntityReference moniker1 = new EntityReference("contact", new Guid(contactGuid));
            EntityReference moniker2 = new EntityReference("account", new Guid(businessGuid));

            EntityReferenceCollection relatedEntities = new EntityReferenceCollection();
            relatedEntities.Add(moniker1);
            request.Target = moniker2;
            request.RelatedEntities = new EntityReferenceCollection { moniker1 };
            request.Relationship = new Relationship("crisp_account_contact");
            service.Disassociate(moniker2.LogicalName, moniker2.Id, request.Relationship, relatedEntities);

To retrieve the records associated between Account and Contact use fetchXml as below.
string fetchXml = @"<fetch version='1.0' " +
                           "output-format='xml-platform' " +
                           "mapping='logical'>" +
                           "<entity name='contact'>" +
                           "<filter type='and'>" +
                               "<condition attribute='statecode' operator='eq' value='0'/>" +
                           "</filter>" +
                           "<link-entity name='crisp_account_contact' from='contactid' to='contactid' visible='false' intersect='true'>" +
                             "<filter type='or'>" +
                               "<condition attribute='accountid' operator='eq' value='" + businessId + "'/>" +
                             "</filter>" +
                          "</link-entity>" +
                          "</entity>" +
                           "</fetch>";

           EntityCollection  entityCollection = service.RetrieveMultiple(new FetchExpression(fetchXml));



You may like below posts

Improving MS CRM Performance

Performance Center in MS CRM 2013

date and time field behavior in MS CRM

Upsert in MSCRM

Locking mechanism in MS CRM

Ticker Symbol Field in MS CRM

Themes in MS CRM

Enable Tracing in MS CRM

Calculated Field in Dynamics CRM 2015

IME Mode in MS CRM

2 comments:

  1. This is deprecated now. Use the new AssociateRequest class, which allows you to associate one record to a collection of related records in a single collection. This is excellent for "select all that apply" situations. https://docs.microsoft.com/en-us/previous-versions/dynamicscrm-2016/developers-guide/gg327615(v=crm.8)

    ReplyDelete
  2. Perfect. Thanks Jazmatician!

    ReplyDelete

Featured Post

Improving MS CRM Performance

Performance on MS CRM is always a crucial thing and we may follow different ways to achieve the performance thing. Below is the one more a...