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

后端 未结 4 1499
忘掉有多难
忘掉有多难 2021-01-14 23:34

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

相关标签:
4条回答
  • 2021-01-14 23:49

    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.

    0 讨论(0)
  • 2021-01-14 23:52

    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);
    
    0 讨论(0)
  • 2021-01-14 23:55

    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

    0 讨论(0)
  • 2021-01-15 00:14

    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)
    
    0 讨论(0)
提交回复
热议问题