2

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?

6 Answers6

1

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

Manish
  • 19
  • 3
  • https://learn.microsoft.com/en-us/powerapps/developer/common-data-service/webapi/associate-disassociate-entities-using-web-api#remove-a-reference-to-an-entity – Jim Daly -MSFT- Apr 29 '20 at 22:34
  • @JimDaly-MSFT- this is the exact one we are using now ie DELETE verb in XMLHttpRequest (I have added in question itself). I’m looking for Xrm.Webapi version actually – Arun Vinoth-Precog Tech - MVP May 20 '20 at 03:10
  • 1
    Xrm.WebApi does what it does, but it doesn't include everything you can do. You can still use XmlHttpRequest without the limitations of Xrm.WebApi. See the JavaScript samples using Sdk.request here: https://learn.microsoft.com/en-us/powerapps/developer/common-data-service/webapi/web-api-samples-client-side-javascript You will find a lot of samples using it nearby. – Jim Daly -MSFT- May 21 '20 at 04:34
  • @JimDaly-MSFT- Ok then I’ll wait for future updates of Xrm.WebApi if any development in this scenario, till then I’ll use the same what I'm doing right now. – Arun Vinoth-Precog Tech - MVP Jun 01 '20 at 05:12
1

I think set the column to null is enough, please make sure you removed '@odata.bind'

var data = { "abc_relatedentity": null };

This is working for me.

0

you should try Xrm.WebApi.online.execute or Xrm.WebApi.online.executeMultiple

var Sdk = window.Sdk || {};


/**
 * Request to execute an update operation
 */
Sdk.UpdateRequest = function(entityName, entityId, payload) {
    this.etn = entityName;
    this.id = entityId;
    this.payload = payload;
};

// NOTE: The getMetadata property should be attached to the function prototype instead of the
// function object itself.
Sdk.UpdateRequest.prototype.getMetadata = function () {
    return {
        boundParameter: null,
        parameterTypes: {},
        operationType: 2, // This is a CRUD operation. Use '0' for actions and '1' for functions
        operationName: "Update",
    };
};

// Construct a request object from the metadata
var payload = {
    "_abc_relatedentity_value": null 
};
var updateRequest = new Sdk.UpdateRequest("abc_entity", abc_entityid, payload);

// Use the request object to execute the function
Xrm.WebApi.online.execute(updateRequest).then(
    function (response) {
       console.log(response)
    },
    function(error) {
        console.log(error.message);
        // handle error conditions
    }
);
-1

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.

maxc137
  • 2,291
  • 3
  • 20
  • 32
-2

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)
Georgy Smirnov
  • 391
  • 3
  • 10
-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);
Ajay Sivan
  • 2,807
  • 2
  • 32
  • 57
  • 1
    I just tested in 1710 (9.1.0.4416) & getting the same error "Property _abc_relatedentity_value cannot be updated to null. The reference property can only be deleted." – Arun Vinoth-Precog Tech - MVP Apr 15 '19 at 15:15
  • 1
    _[LookupFieldName]_value fields are read-only calculated fields – Jim Daly -MSFT- Apr 29 '20 at 22:39
  • @JimDaly-MSFT- thanks Jim, do you know why some people keep saying it is working then - https://community.dynamics.com/crm/f/microsoft-dynamics-crm-forum/310854/update-null-to-lookup-field-using-xrm-webapi-updaterecord/1093357#1093357 – Arun Vinoth-Precog Tech - MVP Sep 22 '20 at 20:43
  • 1
    It probably depends on whether you are using the client-side Xrm.WebApi methods or using the Web API directly. The client API is probably using the autodisassociate header mentioned by others. This is not a supported header. It works, but is not supported. We are working on providing a supported option. When remove it, there will be no notification. So use at your own risk - or just depend on the Xrm.WebAPI methods. – Jim Daly -MSFT- Sep 23 '20 at 21:39