Search code examples
c#dynamics-crmdeprecatedsetstate

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.


Solution

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