How to Update the Status of an Opportunity

By | September 2, 2011

Recently I had the need to update the status of opportunities from a couple of unit tests so I could test a couple of business rules. I looked at the SDK documentation available by Microsoft and their recommendation was to implement all the “SetState” messages based on the example they made available with the SDK which does the update of the Account state.

I used their example and the SetStateOpportunityRequest and SetStateOpportunityResponse classes and ran my unit test just to have it fail with an exception from the CRM server.

After doing some additional research I finally learned that setting the state of an opportunity is a lot different from setting the state of a custom or any other entity and this is not documented in any of Microsoft’s SDK documentation that I know off. That being the case I thought it would be helpful to post it here in case anyone needs it.

This is the fully implemented code which I use on my unit test:

/// <summary>
/// State = Open – Status = 1 “In Progress”, 2 “On Hold”, 6 “Ownership Changed”
/// State = Won – Status = 3 “Won”, 7 “ECOM”
/// State = Lost – Status = 4 “Canceled”, 5 “Out-Sold”
/// </summary>
///opportunityId”>
/// <param name=”state”></param>
/// <param name=”status”></param>

private void UpdateOpportunityState(Guid opportunityId, OpportunityState state, int status)
{
Guid newContactId = Guid.Empty;
WinOpportunityRequest winOpportunityRequest = null;
WinOpportunityResponse winOpportunityResponse = null;
opportunityclose opportunityClose = null;
Lookup lookUp = null;
LoseOpportunityRequest loseOpportunityRequest = null;
LoseOpportunityResponse loseOpportunityResponse = null;
SetStateOpportunityRequest setStateOpportunityRequest = null;
SetStateOpportunityResponse setStateOpportunityResponse = null;
switch (state)
{
case OpportunityState.Open:
{
setStateOpportunityRequest = new SetStateOpportunityRequest();
setStateOpportunityRequest.OpportunityState = state;
setStateOpportunityRequest.OpportunityStatus = status;
setStateOpportunityResponse = (SetStateOpportunityResponse)crmService.Execute(setStateOpportunityRequest);
break;
}
case OpportunityState.Won:
{
winOpportunityRequest = new WinOpportunityRequest();
opportunityClose = new opportunityclose();
lookUp = new Lookup(EntityName.opportunity.ToString(), opportunityId);
opportunityClose.opportunityid = lookUp;
winOpportunityRequest.OpportunityClose = opportunityClose;
winOpportunityRequest.Status = status;
winOpportunityResponse = (WinOpportunityResponse)crmService.Execute(winOpportunityRequest);
break;
}
case OpportunityState.Lost:
{
loseOpportunityRequest = new LoseOpportunityRequest();
opportunityClose = new opportunityclose();
lookUp = new Lookup(EntityName.opportunity.ToString(), opportunityId);
opportunityClose.opportunityid = lookUp;
loseOpportunityRequest.OpportunityClose = opportunityClose;
loseOpportunityRequest.Status = status;
loseOpportunityResponse = (LoseOpportunityResponse)crmService.Execute(loseOpportunityRequest);
break;
}
default:
break;
}
}


											

Leave a Reply