locked
Close Opportunity as Cancelled programmatically RRS feed

  • Question

  • I need to close the opportunity when it's related Account is deactivated. I wrote the plug-in to perform this. Following is part of my plug-in code. I'm getting error on Execute.

    opportunity o = (opportunity)b_entity; 

    Guid opportunityid = new Guid();  

    opportunityid = o.opportunityid.Value;

    SetStateOpportunityCloseRequest request = new SetStateOpportunityCloseRequest();

    request.EntityId = opportunityid;

    request.OpportunityCloseState = OpportunityCloseState.Canceled;

    request.OpportunityCloseStatus = 4;

    SetStateOpportunityCloseResponse closeResponse = service.Execute(request) as SetStateOpportunityCloseResponse;  

    Error is "4 is not a valid status code on opportunityclose."

    Can anyone help? 

    Thursday, December 30, 2010 4:29 AM

Answers

  • Hi Shil,

     

    Try to following code,  

     

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

    Regards,

     

    Ercan Top

    • Marked as answer by shiil Tuesday, January 4, 2011 3:48 PM
    Friday, December 31, 2010 6:39 AM
  • Hi Shill, Visit below URL for more help!
    http://nishantrana.wordpress.com/2008/07/11/closing-an-opportunity-programmatically-crm/ <newline> Thanks and Regards,
    Ankit Shah
    Inkey Solutions, India.
    http://www.inkeysolutions.com/MicrosoftDynamicsCRM.html
    • Marked as answer by shiil Tuesday, January 4, 2011 3:49 PM
    Friday, December 31, 2010 12:57 PM

All replies

  • can you try setting :

    request.OpportunityCloseStatus = -1;

    Thursday, December 30, 2010 7:16 AM
  • When I used -1 for CloseStatus, I got this error.

    ActivityPointer With Id = 2f2ae595-ce11-e011-91f9-005056824704 Does Not Exist.

    The Id there is the OpportunityId and it is available and open.

    Thursday, December 30, 2010 2:42 PM
  • HI,

    You can acheive this without writing any code using this link http://crm40distributewf.codeplex.com/releases/view/40867

     


    Regards Faisal
    Thursday, December 30, 2010 3:33 PM
  • Hi Shil,

     

    Try to following code,  

     

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

    Regards,

     

    Ercan Top

    • Marked as answer by shiil Tuesday, January 4, 2011 3:48 PM
    Friday, December 31, 2010 6:39 AM
  • Hi Shill, Visit below URL for more help!
    http://nishantrana.wordpress.com/2008/07/11/closing-an-opportunity-programmatically-crm/ <newline> Thanks and Regards,
    Ankit Shah
    Inkey Solutions, India.
    http://www.inkeysolutions.com/MicrosoftDynamicsCRM.html
    • Marked as answer by shiil Tuesday, January 4, 2011 3:49 PM
    Friday, December 31, 2010 12:57 PM