Setting null for single-valued navigation property using Xrm.WebApi

倾然丶 夕夏残阳落幕 提交于 2019-12-30 11:29:26

问题


We are in the process of remediation, re-engineering old JS web resources for latest D365 v9 sdk changes w.r.t Client scripting API improvements & deprecation.

When rewriting the web api methods using Xrm.WebApi, we end up with this blocker.

The scenario is setting null to lookup, and tried the below code:

var data = {
    "abc_relatedentity@odata.bind": null
};

Xrm.WebApi.updateRecord("abc_entity", abc_entityid, data).then(successCallback, errorCallback);

This is throwing 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."

The idea is to retire the below redundant XHR request code. But this is the only workaround we have now (referring MSDN).

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();

Anybody faced this & handled it? Am I missing something?


回答1:


To set null on the lookup use:

var data = { _[LookupFieldName]_value : null } 
Xrm.WebApi.updateRecord("abc_entity", abc_entityid, data).then(successCallback, errorCallback

For example to remove contact.parentcustomerid field value you need to use:

var data = {};
data._parentcustomerid_value = null
var t = await Xrm.WebApi.updateRecord("contact", "{0200E6F5-1D21-E811-A954-0022480042B3}", data)



回答2:


I just tried in v9.1.0.3832 var data = { _[LookupFieldName]_value : null } is working for me.

var data =
{
  "statecode": 1,
  "*_myprefix_mylookupfieldname_value*": null
}
Xrm.WebApi.updateRecord("*entityName*", *recordId*, data);


来源:https://stackoverflow.com/questions/49329838/setting-null-for-single-valued-navigation-property-using-xrm-webapi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!