I\'m trying to update the status of open opportunity by using the WinOpportunityRequest & LoseOpportunityRequest API provided in the MSDN. I\'ve f
You are right. State & Status are conjoined twins. You cannot update only one of them, always go in pair.
State = StateCode
Status Reason = StatusCode (field with Padlock)
More read
In your answer code, this is framed correctly in SetStateRequest req.
req.State = new OptionSetValue(0);
req.Status = new OptionSetValue(2);
But in OP, you set only Status not State.
Per MSDN, LoseOpportunityRequest
with OpportunityClose
entity has to close it without issues when you pass only Status. But you are not alone.
Ref: Opportunity & OpportunityClose
For an open opportunity, we can change the status to either win or lose. So we will use the WinOpportunityRequest and LoseOpportunityRequest in here.
So, we need to change the value to -1 so that CRM can load the default status code.
req.Status = new OptionSetValue(4);
after changing to -1 it doesn't throw any exception.
req.Status = new OptionSetValue(-1);
once the execute call is performed. The opportunity value will be changed to lost. The opportunity will be closed.
To re-open the closed opportunity, we can use the SetStateRequest class. The code would be as follows.
var stateRef = new EntityReference("optyname", new Guid("optyid"));
SetStateRequest req = new SetStateRequest();
req.State = new OptionSetValue(0);
req.Status = new OptionSetValue(2);
req.EntityMoniker = stateRef;
SetStateResponse stateSet = (SetStateResponse)_serviceProxy.Execute(req);
After the execute call is performed the opportunity status is set back to open and the status is displayed as open.
state code is different from status. state code can have open, win or close. status can have multiple values. detailed info is provide at msdn.