2

我们正在修复,重新设计旧的 JS Web 资源,以用于最新的 D365 v9 sdk 更改 wrt 客户端脚本 API 改进和弃用。

当使用 重写 web api 方法Xrm.WebApi时,我们最终得到了这个阻止程序。

场景设置null为查找,并尝试了以下代码:

var data = {
    "abc_relatedentity@odata.bind": null
};

Xrm.WebApi.updateRecord("abc_entity", abc_entityid, data).then(successCallback, errorCallback);

这是抛出错误:

“'odata.bind' 实例或属性注释具有空值。在 OData 中,'odata.bind' 实例或属性注释必须具有非空字符串值。”

这个想法是淘汰下面的冗余 XHR 请求代码。但这是我们现在唯一的解决方法(参考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();

有人遇到过并处理过吗?我错过了什么吗?

4

6 回答 6

1

您必须使用 Delete 方法删除查找值格式如下:/api/data/v8.0/accounts(1DD18913-11CB-E511-80D2-C4346BDC11C1)/primarycontactid/$ref

于 2020-01-15T17:52:22.677 回答
1

我认为将列设置为 null 就足够了,请确保您删除了 '@odata.bind'

var data = { "abc_relatedentity": null };

这对我有用。

于 2021-08-03T03:01:23.483 回答
0

您应该尝试 Xrm.WebApi.online.execute 或 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
    }
);
于 2021-05-07T10:03:21.407 回答
-1

好消息!null您可以通过将此标头添加到PATCH请求中来将查找字段设置为请求中。

autodisassociate: true

然后你可以使用这样的东西以任何方式改变你的查找字段:

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);
    }
}

OPXMLHttpRequest无论如何都在使用,所以我认为,在这里使用的方法PATCH是相关的。

于 2020-06-10T08:07:12.220 回答
-2

要在查找上设置 null,请使用:

var data = { _[LookupFieldName]_value : null } 
Xrm.WebApi.updateRecord("abc_entity", abc_entityid, data).then(successCallback, errorCallback

例如,要删除contact.parentcustomerid您需要使用的字段值:

var data = {};
data._parentcustomerid_value = null
var t = await Xrm.WebApi.updateRecord("contact", "{0200E6F5-1D21-E811-A954-0022480042B3}", data)
于 2018-03-22T11:38:18.897 回答
-2

我刚刚尝试在 v9.1.0.3832 var data = { _[LookupFieldName]_value : null }中为我工作。

var data =
{
  "statecode": 1,
  "*_myprefix_mylookupfieldname_value*": null
}
Xrm.WebApi.updateRecord("*entityName*", *recordId*, data);
于 2019-04-12T03:51:50.800 回答