Associate and disassociate request is used to link and unlink records having relationship. We can link and unlink records depending on their relationship type for example, We can link records having 1:N using update request (setting lookup). In our earlier sample we demonstrated how we can update lookup field using Web API. In this post we are going to discuss associating and disassociating N:N relationship.
Let’s say we have a custom entity called Building and which is related to account using N:N relationship, so a single account can be related to multiple buildings, similarly single building can be related to multiple accounts.
So if we need to associate account record with building entity record, we can write POST request using following syntax can pass target record id as object:
[OrganizationURL]+"/api/data/v8.0/accounts(0370F447-C9B2-E511-80DF-3863BB35DED8)/him_building_account/$ref"
Where him_building_account is the name of the relationship, we can check it by opening N:N relationship record:
We can use following code in our java script web resource for associate request:
function associateRequest(accountID, buildingID) { var associate = { "@odata.id": serverURL + "/api/data/v8.0/him_buildings(" + buildingID + ")" }; var serverURL = Xrm.Page.context.getClientUrl(); var req = new XMLHttpRequest(); req.open("POST", serverURL + "/api/data/v8.0/accounts(" + accountID + ")/him_building_account/$ref", true); req.setRequestHeader("Accept", "application/json"); req.setRequestHeader("Content-Type", "application/json; charset=utf-8"); req.setRequestHeader("OData-MaxVersion", "4.0"); req.setRequestHeader("OData-Version", "4.0"); req.onreadystatechange = function() { if (this.readyState == 4 /* complete */ ) { req.onreadystatechange = null; if (this.status == 204) { alert('Record Associated'); } else { var error = JSON.parse(this.response).error; alert(error.message); } } }; req.send(associate); }
And to disassociate records we can write following request using DELETE method:
function disassociateRequest(accountID, buildingID) { var serverURL = Xrm.Page.context.getClientUrl(); var req = new XMLHttpRequest(); req.open("DELETE",serverURL+"/api/data/v8.0/accounts("+accountID+")/him_building_account/$ref?$id="+serverURL+"/api/data/v8.0/him_buildings("+buildingID+")", true); req.setRequestHeader("Accept", "application/json"); req.setRequestHeader("Content-Type", "application/json; charset=utf-8"); req.setRequestHeader("OData-MaxVersion", "4.0"); req.setRequestHeader("OData-Version", "4.0"); req.onreadystatechange = function() { if (this.readyState == 4 /* complete */ ) { req.onreadystatechange = null; if (this.status == 204) { alert('Record Disassociated'); } else { var error = JSON.parse(this.response).error; alert(error.message); } } }; req.send(); }
Note: You can get complete system and custom entity reference for Web API using
[Organizaiton URL] + “/api/data/v8.0/
Stay tuned for more Web API Samples !!
Pingback: Writing associate & disassociate request using Web API - Microsoft Dynamics CRM Community
Really heplull artical. Just need to change last line whine sending request user JSON.stringify(associate).
Hi,Can you please help me to understand N: N retrieve multiple in crm.
using web api
Hi
When I use the above approach I am getting error
Invalid role specified for entity “contact” in “relationshipname” relatioship
Make sure you are passing correct relationship name.
I am getting the same error, I’ve checked the relationshipname and it is ok, can you fix this error?
Hi,
I still have error
I want clear lookupvalue
this – work fine
DELETE http://siteName/api/data/v8.2/accounts(780074FF-8120-E911-90F3-005056874F50)/primarycontactid/$ref
but this
http://siteName/api/data/v8.2/tasks(2419C488-FF35-EA11-911E-005056873F1D)/regardingobjectid/$ref
– not((
with error:
“The URI segment ‘$ref’ is invalid after the segment ‘regardingobjectid’.
Is there a way to associate multiple values in single call?
I am using subgrid with many-to-many relation ship and wants to associate multiple records
Hi Jignesh, thanks for reading our blog, you can pass multiple data to request and perform association, please refer : https://docs.microsoft.com/en-us/powerapps/developer/common-data-service/webapi/associate-disassociate-entities-using-web-api
I already referred link which you provided before posting query on you blog. In that link they have given example for “1 to many relationship”. I want to perform same for “N to N relationship”. I tried that example but not working. Is there any other solution?
Thanks!