简体   繁体   中英

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

How do I reopen a closed (won or lost) opportunity via C# code in Dynamics CRM? SetStateRequest is deprecated (see documentation ), and when I try to do the same via an Update I get this error:

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'.

The code for this is:

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

Executing a REST PATCH request to set statecode and statuscode produces the same error.

I temporarely deactivated all plugins running on opportunities to make sure that those aren't the cause for this error.

This seems to be an exception for opportunities. Other records may be closed and opened using Update without any issues.

I tested this on CRM 8.2 and 9.1.

There are 2 ways you could so.

  1. Using c#

    For c# is just updating statecode to 0

  2. Using REST (WEBAPI)

For Webapi just using PATCH method and set statecode to 0

Now how do you call it via Webapi. Here is the sample Code From front end Side to call it. You can easily replicate this using Postman and see how this helps.

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

So after trying this on another tenant where it worked I came to the conclusion that there has to be some component responsible for this error. Lo and behold, there is a third party plugin registered on RetrieveMultiple of any Entity. Upon deactivating this plugin the Update works as intended.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM