I am sure that there is another blog that has posted code like this before, but I was cleaning up a project this morning, saw these two methods, and decided to just post them here so I could find them later. :)

These are in C#.  For more information on how they work, please review the CRM SDK.

 

private void CloseOpportunity(Guid opportunityId, int statusCode, string description)
{
    opportunityclose oppclose = new opportunityclose();
    oppclose.opportunityid = new Lookup();
    oppclose.opportunityid.type = EntityName.opportunity.ToString();
    oppclose.opportunityid.Value = opportunityId;
    oppclose.description = description;
    oppclose.actualend = new CrmDateTime(DateTime.Now.ToString());

    WinOpportunityRequest woReq = new WinOpportunityRequest();
    woReq.OpportunityClose = oppclose;
    woReq.Status = 3;   // Won;

    crmService.Execute(woReq);
}

private void CancelOpportunity(Guid opportunityId, int statusCode, string description)
{
    opportunityclose oppclose = new opportunityclose();
    oppclose.opportunityid = new Lookup();
    oppclose.opportunityid.type = EntityName.opportunity.ToString();
    oppclose.opportunityid.Value = opportunityId;
    oppclose.description = description;
    oppclose.actualend = new CrmDateTime(DateTime.Now.ToString());

    LoseOpportunityRequest woReq = new LoseOpportunityRequest();
    woReq.OpportunityClose = oppclose;
    woReq.Status = statusCode;

    crmService.Execute(woReq);
}

 

One Response to Closing and Canceling Opportunities

  1. Thomas Rehm says:

    Hi,

    everywhere in the inet the code for marking an opportunity as won or lost is very similar to yours.

    But i am having a problem with this approach.

    When i win/lose an opportunity the first time, everything is fine as it should.

    I Can reopen it on the UI or via SetStateRequest. But when i when re-won/re-lose it with all the codes from the inet, i get an error, saying that he cant update closed or canceled records. That is because the "CloseOpportunity"-Activity gots marked as canceled while reopening.

    do you have any idea, what i have to change so that an re-won/re-lose will work after an re-open?

    On the UI it is not a problem, only via code

    Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>