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

后端 未结 4 1534
忘掉有多难
忘掉有多难 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.

提交回复
热议问题