A method in Dynamics 365 to set the value of a lookup field to a null value using Web API.

Abstract: My WeChat and Yixin official account: Rayong, Microsoft dynamic CRM expert, reply 270 or 20180424 to get this article easily, and at the same time you can get the latest blog post information published by me in the first room, follow me! My website is www.luoyong.me.

Dynamics CRM 2016 began to recommend the use of Web API, I also have a series of blog articles to introduce, the main articles are as follows:

But how do I set the lookup field's value to null via Web API? We know that it is enough to set the field value to NULL when programming the form and using the organization service. Can the same be true for the Web API? The answer is no.

The method can refer to Microsoft's document: Associate and disassociate entities using the Web API , I won't say much here, just use an example to demonstrate.

I want to set the value of [Rayong Test] entity schema name to [ly_Lookup] (type is lookup field) to be empty, you can see that this field has a value before setting:

 

 

I use the following code to set the value of the search field to be empty. There are two things worth noting: one is that the HTTP method is DELETE, and the other is that the name of the field uses the schema name:

var clientURL = Xrm.Page.context.getClientUrl();
var req = new XMLHttpRequest()
req.open("DELETE", encodeURI(clientURL + "/api/data/v8.2/ly_tests(B907DE1B-CF99-E611-8161-000D3A80C8B8)/ly_Lookup/$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 deleted successfully!" );
            }
            else if (this.status == 404) {
                alert( "The record to be deleted does not exist!" )
            }
            else {
                var error = JSON.parse(this.response).error;
                alert( "Failed to delete the lookup field value of the Rayong test entity record." + error.message);
            }
        }
    };
req.send();

After execution, you can see that the value of the search field is empty.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324824431&siteId=291194637