Microsoft CRM:如何通过代码重新打开商机? SetStateRequest 已弃用,更新不起作用

Microsoft CRM: How to reopen an opportunity by code? SetStateRequest deprecated, Update not working

如何在 Dynamics CRM 中通过 C# 代码重新打开已结束(赢得或失去)的机会? SetStateRequest 已弃用(请参阅 documentation),当我尝试通过 Update 执行相同操作时,出现此错误:

Element 'http://schemas.microsoft.com/2003/10/Serialization/Arrays:anyType' contains data from a type that maps to the name 'Microsoft.Crm.Common.ObjectModel:ActivityState'. The deserializer has no knowledge of any type that maps to this name. Consider changing the implementation of the ResolveName method on your DataContractResolver to return a non-null value for name 'ActivityState' and namespace 'Microsoft.Crm.Common.ObjectModel'.

代码是:

Entity updateTarget = new Entity("opportunity", opportunityId);
updateTarget["statecode"] = new OptionSetValue(0); // 0 = Open
updateTarget["statuscode"] = new OptionSetValue(1); // 1 = In Progress
orgSvc.Update(updateTarget); // Raises exception

执行 REST PATCH 请求以设置状态代码和状态代码会产生相同的错误。

我暂时停用了所有插件 运行 以确保这些不是导致此错误的原因。

这似乎是机会的例外。其他记录可以使用 Update 关闭和打开,没有任何问题。

我在 CRM 8.2 和 9.1 上对此进行了测试。

有两种方法可以实现。

  1. 使用 c#

    因为 c# 只是将状态码更新为 0

  2. 使用 REST (WEBAPI)

对于 Webapi,只需使用 PATCH 方法并将状态码设置为 0

现在如何通过 Webapi 调用它。这是从前端端调用它的示例代码。您可以使用 Postman 轻松复制它,看看它有什么帮助。

var entity = {};
entity.statecode = 0;

var req = new XMLHttpRequest();
req.open("PATCH", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/opportunities(8CA20837-715F-E911-A83A-000D3A3852A3)", 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));

所以在另一个租户上尝试了这个之后,我得出的结论是必须有一些组件导致这个错误。你看,任何实体的 RetrieveMultiple 上都注册了第三方插件。停用此插件后,更新将按预期工作。