Quantcast
Channel: C# – Microsoft Dynamics CRM Blog
Viewing all articles
Browse latest Browse all 14

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

$
0
0

If we have any N:N(Many to Many) relationship in Microsoft Dynamics CRM 2011, need to manually assign relationship between two entities using SDK.

This example explains how to associate and disassociate N:N(Many to Many) relationship records in CRM 2011 thru SDK. In my example I have N:N relationship between Contact and Account. Relationship name is ls_contact_account.


       // Creates the custom many-to-many relationship between the contact and account.
        public void CreateContactAccountRelationship(IOrganizationService service, EntityReference accountRef, EntityReference contactRef)
        {
            //If one of the ID's is null, do nothing
            if (accountRef == null) return;
            if (contactRef == null) return;
            if (accountRef.LogicalName != "account") return;
            if (contactRef.LogicalName != "contact") return;

            var accountID = accountRef.Id;
            var contactId = contactRef.Id;

            //The relationship schema to create
            string relationshipName = "ls_contact_account";

            //Create a query that will check to see if the relationship already exists between this account and contact
            QueryExpression query = new QueryExpression(relationshipName)
            {
                NoLock = true,
                ColumnSet = new ColumnSet(false),//only get the row ID, since we don't need any actual values
                Criteria =
                {
                    Filters =
			        {
				        new FilterExpression
				        {
					        FilterOperator = LogicalOperator.And,
					        Conditions =
					        {
                                //Get the row for the relationship where the account and contact are the account and contact passed in
						        new ConditionExpression("accountid", ConditionOperator.Equal, accountID.ToString()),
						        new ConditionExpression("contactid", ConditionOperator.Equal, contactId.ToString()),
					        },
				        },
			        }
                }
            };
            var result = service.RetrieveMultiple(query);
            //Check if the relationship was not found
            if (result == null || result.Entities == null || result.Entities.Count < 1)
            {
                //The relationship was not found, so create it
                service.Associate(accountRef.LogicalName, accountRef.Id, new Relationship(relationshipName), new EntityReferenceCollection() { contactRef });
            }
        }

       // Deletes the many-to-many relationship record between the contact and account.
        public void DeleteContactAccountRelationship(IOrganizationService service, EntityReference accountRef, EntityReference contactRef)
        {
            //If one of the ID's is null, do nothing
            if (accountRef == null) return;
            if (contactRef == null) return;
            if (accountRef.LogicalName != "account") return;
            if (contactRef.LogicalName != "contact") return;

            var accountID = accountRef.Id;
            var contactId = contactRef.Id;

            //The relationship schema to create
            string relationshipName = "ls_contact_account";

            //Create a query that will check to see if the relationship already exists between this account and contact
            QueryExpression query = new QueryExpression(relationshipName)
            {
                NoLock = true,
                ColumnSet = new ColumnSet(false),//only get the row ID, since we don't need any actual values
                Criteria =
                {
                    Filters =
			        {
				        new FilterExpression
				        {
					        FilterOperator = LogicalOperator.And,
					        Conditions =
					        {
                                //Get the row for the relationship where the account and contact are the account
						        new ConditionExpression("accountid", ConditionOperator.Equal, accountID.ToString()),
						        new ConditionExpression("contactid", ConditionOperator.Equal, contactId.ToString()),
					        },
				        },
			        }
                }
            };
            var result = service.RetrieveMultiple(query);
            //check if record exists
            if (result != null && result.Entities != null && result.Entities.Count > 0)
            {
                //Delete the N:N relation
                service.Disassociate(accountRef.LogicalName, accountRef.Id, new Relationship(relationshipName), new EntityReferenceCollection() { contactRef });
            }
        }

Viewing all articles
Browse latest Browse all 14

Trending Articles