Hello everyone and welcome to my blog. In today’s blog I will show how you can associate records in N:N relationship in DataVerse/ Dynamics 365 using Xrm.WebApi client API’s.
Microsoft have introduced Xrm.WebApi sometime back and it was indeed a great step-up from the former ways of performing server side operations using Client API in DataVerse/ Dynamics 365. When I received this requirement, I was pretty casual that I would be able to spin that up in minutes. But to my surprise it turned out to be little more complex than I expected.
So let’s get started. The first step is to create the N:N associate request. And below is the sample code for associating a security role to the user (N:N relationship)
var manyToManyRequest = {};
manyToManyRequest.getMetadata = function () {
return {
boundParameter: null,
parameterTypes: {},
operationType: 2,
operationName: "Associate"
};
};
manyToManyRequest.relationship = "systemuserroles_association"; //schema name of N:N relationship
manyToManyRequest.target = { entityType: "systemuser", id: "<user_guid>" };
manyToManyRequest.relatedEntities = [
{ entityType: "role", id: "<role1_id>" },
{ entityType: "role", id: "<role2_id>" }
];
Let’s look at some important properties of the request. If you are accustomed to using Xrm.WebApi to execute actions, you are aware of the getMetadata function. Important here to note is operationType is 2 and operationName is Associate.
The next important property is relationship. You need to set it to the schema name of N:N relationship.
The final two properties are target and relatedEntities. Essentially this is where you specify the records of participating entities in the relationship. In my example, the participating entities are systemuser and role.
Well there you go. The final step is to pass this request to the framework and to do this, use the below code.
Xrm.WebApi.online.execute(manyToManyRequest).then(
(success) => {
console.log("Success");
},
(error) => {
console.log("Error", error);
}
)
Hope this helped. You will also like the below posts.
Debajit Dutta
Discover more from Debajit's Power Apps & Dynamics 365 Blog
Subscribe to get the latest posts sent to your email.