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.
W
Great news! You can set lookup field to null
in PATCH
request by adding this header to your request.
autodisassociate: true
And then you can use something like this to alter your lookup field in any way:
SetLookupField(requestBody, "systemusers", "msdfm_MyUser", null)
// Or
SetLookupField(requestBody, "systemusers", "msdfm_MyUser", "f5b0b514-aea8-ea11-a812-000d3a569fe1")
// ...
private static void SetLookupField(JObject requestBody, string typePlural, string name, string value)
{
if (!string.IsNullOrEmpty(value))
{
requestBody.Add($"{name}@odata.bind", $"/{typePlural}({value})");
}
else
{
requestBody.Add($"_{name.ToLower()}_value", null);
}
}
OP uses XMLHttpRequest
anyway, so I thought, a way to do this using PATCH
will be relevant here.
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);
You have to use Delete method to remove the lookup value format is as follows: /api/data/v8.0/accounts(1DD18913-11CB-E511-80D2-C4346BDC11C1)/primarycontactid/$ref
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)