Topics

Knowledge Creation Laboratory by CRM(xRM)

Dynamics 365 (Dynamics CRM) Web API Record Deletion/Association

Hello everyone.
It's hot, it's piping hot, it's scorching hot (lol)
Since it's so hot, I looked up UberEATS, where my favorite McDonald's can be delivered lol
But I can't pay in cash so I'm turned down.

The Web API series is nearing the end. This time, I will write about deleting, associating, and unassociating records.

Dynamics 365 record deletion basics

Deleting a record is very simple: just make a request to a URL that identifies the record using the HTTP method "DELETE".
So, it looks like this:

var requestUrl = "https://<orgname>.crm7.dynamics.com/api/data/v8.2/accounts(394BD9A2-3945-E711-8110-C4346BC53068)";
var req = new XMLHttpRequest();
req.open("DELETE", encodeURI(requestUrl), 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) {
Xrm.Utility.alertDialog("Record deleted!", null);
} Else {
var error = JSON.parse(this.response).error;
Xrm.Utility.alertDialog(error.message, null);
}
}
};
req.send(null);

It's pretty easy once you identify the records you want to delete.

Dynamics 365 record association basics

Record association means creating 1:N, N:1, and N:N associations. Single-valued navigation properties are used to create N:1 associations, and collection-valued navigation properties are used to create 1:N and N:N associations.
Single-valued navigation properties have already been implemented by setting up search fields in the record creation and update sections, so here we will provide an example of how to associate using collection-valued navigation properties.

Here, we want to associate a contact person (contact) who exists in a 1:N relationship with a business partner (account). Here is an example.

var contact = {};
// GUID of the customer contact you want to associate
contact["@odata.id"] = "/contacts(5d4936f4-c95e-e711-80f6-480fcff207f1)";
var apiUrl = "https://ORGNAME.crm7.dynamics.com/api/data/v8.2";
var record = "/accounts(7d612113-c75e-e711-80f6-480fcff207f1)";
// Specify the navigation property name here.
var navProp = "/contact_customer_accounts/$ref";
var requestUrl = apiUrl + record + navProp;
var req = new XMLHttpRequest();
req.open("POST", encodeURI(requestUrl), 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) {
Xrm.Utility.alertDialog("Association successful!", null);
} Else {
var error = JSON.parse(this.response).error;
Xrm.Utility.alertDialog(error.message, null);
}
}
};
req.send(JSON.stringify(contact));

It looks something like this. This 1:N relationship is an N:1 relationship from the perspective of the contact, so it has the same result as setting the value of the search field in the record creation and update sections. This method also allows for N:N relationships, so give it a try! Lol

For single-valued navigation properties, the same can be achieved by using the HTTP method "PUT".

Dynamics 365 record disassociation basics

Now that the association is complete, the next step is to remove the association. Here's a quick example!

var apiUrl = "https://ORGNAME.crm7.dynamics.com/api/data/v8.2";
var record = "/accounts(7d612113-c75e-e711-80f6-480fcff207f1)";
// Specify the navigation property name and the GUID of the target record here.
var navProp = "/contact_customer_accounts(5d4936f4-c95e-e711-80f6-480fcff207f1)/$ref";
var requestUrl = apiUrl + record + navProp;
var req = new XMLHttpRequest();
req.open("DELETE", encodeURI(requestUrl), 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) {
Xrm.Utility.alertDialog("Association removal successful!", null);
} Else {
var error = JSON.parse(this.response).error;
Xrm.Utility.alertDialog(error.message, null);
}
}
};
req.send(null);

You can remove the association by modifying the URL to specify the GUID to be removed, with the HTTP method set to "DELETE". For single-valued navigation properties, the URL to be removed in the sample above is not necessary.

Next notice

I have explained the Web API several times so far. I have received several requests, so next time I would like to write about paging and record retrieval using FetchXML as an extension to the record retrieval section.

*The content of this article is for personal reference only. Please use the content of this article at your own discretion.

Person who wrote this article
Kengo Harada

Liberal arts programmer (.NET Framework, Java).
I started this program at the age of 25 and worked as hard as I could (I praise myself because no one else praises me).
Before I knew it, I was the manager of the product development department...
Our companyDynamics CRM TrainingI also work as a lecturer.
There are business divisions and job titles, but the title that best suits me is "handyman within the business division."
Arcus Japan Co., Ltd. CRM Products
CRM product inquiries
TEL +06-6195-7501-XNUMX
CONTACT

Articles in the same category