Sitemap

Tuesday, July 8, 2014

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

No comments:

Post a Comment