无法通过 Web API 客户端关闭事件

Can't close Incident through Web API client side

在 Dynamics 365 上,我们正在尝试使用客户端 Web 关闭事件 API。

查看文档(使用 C#)后,我们了解到我们首先需要创建一个 IncidentResolution activity,我们成功地做到了。 但是,我们不明白如何完全关闭事件实体。

我假设我们需要更新记录的 stateCode 和 statusCode。但是,如果我这样做,ajax 总是 return 500 错误。

其他更新正常。

这里有什么我们遗漏的吗?

var entity = {};
entity.statecode = 1; // Resolved
entity.statuscode = 5; // Problem Solved
entity.title = "Title of my case";

var req = new XMLHttpRequest();
req.open("PATCH", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/incidents(Case's guid)", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.onreadystatechange = function() {
    if (this.readyState === 4) {
        req.onreadystatechange = null;
        if (this.status === 204) {
            //Success - No Return Data - Do Something
        } else {
            Xrm.Utility.alertDialog(this.statusText);
        }
    }
};
req.send(JSON.stringify(entity)); 

您必须使用 CloseIncident Action & POST 方法来执行此操作。这不是使用 PATCH 方法的简单更新请求,基本上案例关闭将创建一个 Incident Resolution 实体记录。

通常我会使用 CRM REST 构建器编写请求,即使在这种情况下该代码段未成功执行。完整的工作代码示例:

var incidentresolution = {
    "subject": "Put Your Resolve Subject Here",
    "incidentid@odata.bind": "/incidents(<GUID>)", //Replace <GUID> with Id of case you want to resolve
    "timespent": 60, //This is billable time in minutes
    "description": "Additional Description Here"
};

var parameters = {
    "IncidentResolution": incidentresolution,
    "Status": -1
};

var context;

if (typeof GetGlobalContext === "function") {
    context = GetGlobalContext();
} else {
    context = Xrm.Page.context;
}

var req = new XMLHttpRequest();
req.open("POST", context.getClientUrl() + "/api/data/v8.2/CloseIncident", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.onreadystatechange = function () {
    if (this.readyState === 4) {
        req.onreadystatechange = null;
        if (this.status === 204) {
            //Success - No Return Data - Do Something
        } else {
            var errorText = this.responseText;
            //Error and errorText variable contains an error - do something with it
        }
    }
};
req.send(JSON.stringify(parameters));

Reference