I need to use XrmQuery to update an id-binding field with "null".
Here is my code:
XrmQuery.update(x => x.contacts,
recordId,
{
address2_line1: null,
address2_postalcode: null,
address2_city: null,
cgk_origindate: null,
cgk_countrypostaladdressid_bind$cgk_countries: null,
cgk_originaddress: null
}).execute(id => {
});
The update does not work with cgk_countrypostaladdressid_bind$cgk_countries: null
.
When I delete this line the update works fine. Is there a typical way to make an id-binding field "null" with XrmQuery?
You cannot set lookup fields to null by sending an update-request to the Web API. Instead, you need to send a disassociate request. Unfortunately we do not yet currently support associate/disassociate requests in XrmQuery.
Until we have built support for this directly into XrmQuery (issue 31), you can manually craft the request as follows:
As you can see on the MSDN-page, you need to send an HTTP DELETE
in a specific format. For lookups (which are called single-valued navigation properties) you can set their value to null as follows:
XrmQuery.sendRequest("DELETE",
"contacts(" + recordId + ")/cgk_countrypostaladdressid/$ref",
null,
() => { alert("success"); },
() => { alert("error"); }
);