Clearing single valued navigation property aka Lookup field using Dynamics CRM Web API
Views (1836)
Like any other attribute we cannot use CRM WEB API PATCH verb to update the lookup or single valued navigation property to NULL. In other words we can replace the old value with a new value in lookup but unable to clear the existing value.
You can refer my below question in Stack Overflow, still unanswered.
Setting null for single-valued navigation property using Xrm.WebApi
When you use this below snippet,
"The 'odata.bind' instance or property annotation has a null value. In OData, the 'odata.bind' instance or property annotation must have a non-null string value."
If you use the below snippet,
"Property _myprefix_mylookupfieldname_value cannot be updated to null. The reference property can only be deleted."
Microsoft documentation says
So the only available solution is using DELETE verb with XMLHttpRequest along with $ref keyword.
You can refer my below question in Stack Overflow, still unanswered.
Setting null for single-valued navigation property using Xrm.WebApi
When you use this below snippet,
var data = {
"abc_relatedentity@odata.bind": null
};
Xrm.WebApi.updateRecord("abc_entity", abc_entityid, data).then(successCallback, errorCallback);it will fail with the error:"The 'odata.bind' instance or property annotation has a null value. In OData, the 'odata.bind' instance or property annotation must have a non-null string value."
If you use the below snippet,
var data =
{
"_myprefix_mylookupfieldname_value": null
}
Xrm.WebApi.updateRecord("entityName", recordId, data);you will get this error:"Property _myprefix_mylookupfieldname_value cannot be updated to null. The reference property can only be deleted."
Microsoft documentation says
Use a DELETE request to remove a reference to an entity. The way you do it is different depending on whether you’re referring to a collection-valued navigation property or a single-valued navigation property.This is an age old issue.
So the only available solution is using DELETE verb with XMLHttpRequest along with $ref keyword.
var req = new XMLHttpRequest();
req.open("DELETE", Xrm.Utility.getGlobalContext().getClientUrl() + "/api/data/v9.0/accounts(recordGUID)/account_parent_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) {
req.onreadystatechange = null;
if (this.status === 204 || this.status === 1223) {
//Success - No Return Data - Do Something
}
}
};
req.send();This was originally posted here.

Like
Report
*This post is locked for comments