locked
Is is possible to change the State of a Quote RRS feed

  • Question

  • Is is possible to change the state of a quote from Active/Draft to Closed though plugins. I was been able to change the state from draft to active successfully using SetStateRequest. However, changing the state from active to closed is throwing an error.  Any help on this is greatly appreciated. Thanks
    Wednesday, January 2, 2013 5:57 PM

Answers

  • Something like this for a Quote:

    CloseQuoteRequest request = new CloseQuoteRequest();
    Entity quoteClose = new Entity("quoteclose");
    quoteClose.Attributes.Add("quoteid", new EntityReference("quote", new Guid("C821F444-0855-E211-8C59-00505685006C")));
    quoteClose.Attributes.Add("subject", "Enter something here");
    request.QuoteClose = quoteClose;
    request.RequestName = "CloseQuoteRequest";
    request.Status = new OptionSetValue(5); //Lost
    CloseQuoteResponse response = (CloseQuoteResponse)_serviceProxy.Execute(request);


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    • Proposed as answer by JLattimerMVP, Moderator Wednesday, January 2, 2013 6:27 PM
    • Unproposed as answer by valCrmDev2 Thursday, January 3, 2013 3:06 PM
    • Marked as answer by valCrmDev2 Thursday, January 3, 2013 4:10 PM
    Wednesday, January 2, 2013 6:27 PM
    Moderator
  • Hi,

    Instead of using SetStateRequest to activate your quote, you should use SetStateQuoteRequest for Activating the Quote, but once is active you close it as won using CloseQuoteRequest.

    SetStateQuoteRequest NewSr = new SetStateQuoteRequest();
    NewSr.EntityId = theQuote.quoteid.Value;
    NewSr.QuoteState = QuoteState.Active;
    NewSr.QuoteStatus = 3;
    SetStateQuoteResponse response = new SetStateQuoteResponse();
    response = theService.Execute(NewSr);
    //Quote is active now
    
    quoteclose myquoteclose = new quoteclose();
    myquoteclose.quoteid = new Lookup();
    myquoteclose.quoteid.type = EntityName.quote.ToString();
    myquoteclose.quoteid.Value = YOURQUOTEGUID;
    
    CloseQuoteRequest quotecloserequest = new CloseQuoteRequest();
    quotecloserequest.QuoteClose = myquoteclose;
    quotecloserequest.Status = 5;
    //quote is closed
    
    I use this after I have activated the quote using SetStateQuoteRequest to QuoteState.Active. And I only activate it if the quote is Draft (statuscode.Value == 1). If is not draft, then you can close it directly or it could give an error trying to activate an already active Quote.


    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful" Thanks, Imran Abbasi

    • Marked as answer by valCrmDev2 Thursday, January 3, 2013 4:10 PM
    Thursday, January 3, 2013 1:24 AM

All replies

  • Something like this for a Quote:

    CloseQuoteRequest request = new CloseQuoteRequest();
    Entity quoteClose = new Entity("quoteclose");
    quoteClose.Attributes.Add("quoteid", new EntityReference("quote", new Guid("C821F444-0855-E211-8C59-00505685006C")));
    quoteClose.Attributes.Add("subject", "Enter something here");
    request.QuoteClose = quoteClose;
    request.RequestName = "CloseQuoteRequest";
    request.Status = new OptionSetValue(5); //Lost
    CloseQuoteResponse response = (CloseQuoteResponse)_serviceProxy.Execute(request);


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    • Proposed as answer by JLattimerMVP, Moderator Wednesday, January 2, 2013 6:27 PM
    • Unproposed as answer by valCrmDev2 Thursday, January 3, 2013 3:06 PM
    • Marked as answer by valCrmDev2 Thursday, January 3, 2013 4:10 PM
    Wednesday, January 2, 2013 6:27 PM
    Moderator
  • Hi Jason,

    Thank you very much for you response. I have tried your suggestion but I still get the same error. I have attached a section my code so you can see what I am trying to do. Please let me know what I am doing incorrectly or what I am missing. Thank you.

       if (opportunity.Attributes.Contains("triggercloseaslostplugin"))
                            {
    
                                //use the service to get all proposals related to the opportunity 
                                using (var crm = new ServiceContext(service))
                                {
    
                                    //Create a list of all the proposals/quotes that are either drafts or active
                                    //var proposals = crm.QuoteSet.Where(q => q.OpportunityId.Id == opptyId && (q.StatusCode.Value == 0 || q.StatusCode.Value == 1)).ToList();
    
                                    var proposals = crm.QuoteSet.Where(q => q.OpportunityId.Id == opptyId && (q.StateCode == 0 || (int)q.StateCode == 1)).ToList();
                                    //throw new InvalidPluginExecutionException(" The trigger field was updated: There are " + proposals.Count() + " proposals associated with this opp # " + opptyId);
    
                                    foreach (Quote quote in proposals)
                                    {
    
                                        //throw new InvalidPluginExecutionException(" The trigger field was updated: There are " + proposals.Count() + " proposals associated with this opp # " + opptyId);
                                        OptionSetValue currentQuotestatecode = (OptionSetValue)quote["statecode"];
                                        OptionSetValue currentQuotestatuscode = (OptionSetValue)quote["statuscode"];
    
                                        //throw new InvalidPluginExecutionException(" The logical name is " + quote.LogicalName);
                                        //throw new InvalidPluginExecutionException(" The statecode is " + currentQuotestatecode.Value + " status reason is  " + currentQuotestatuscode.Value);
    
                                        if (currentQuotestatecode.Value == 0)
                                        {
                                            SetStateRequest setStateRequest = new SetStateRequest();
                                            setStateRequest.EntityMoniker = new EntityReference();
                                            setStateRequest.EntityMoniker.Id = quote.Id;
                                            setStateRequest.EntityMoniker.LogicalName = "quote";
                                            //Set the quote status
                                            setStateRequest.State = new OptionSetValue(3);
                                            setStateRequest.Status = new OptionSetValue(-1);
                                            crm.Execute(setStateRequest);
                                            // throw new InvalidPluginExecutionException("Success from draft to Active!!!");
                                        }
                                        else
                                        {
                                            SetStateRequest setStateRequest = new SetStateRequest();
                                            setStateRequest.EntityMoniker = new EntityReference();
                                            setStateRequest.EntityMoniker.Id = quote.Id;
                                            setStateRequest.EntityMoniker.LogicalName = "quote";
    
                                            //Create the quoteclose status
                                            CloseQuoteRequest request = new CloseQuoteRequest();
                                            Entity quoteClose = new Entity("quoteclose");
                                            quoteClose.Attributes.Add("quoteid", new EntityReference("quote", quote.Id));
                                            quoteClose.Attributes.Add("subject", "Created by plugin");
                                            request.QuoteClose = quoteClose;
                                            request.RequestName = "closequoterequest";
                                            request.Status = new OptionSetValue(5); //Lost
    
                                            //Set the quote status
                                            setStateRequest.State = new OptionSetValue(3);
                                            setStateRequest.Status = new OptionSetValue(-1);
                                            //CloseQuoteResponse response = (CloseQuoteResponse)crm.Execute(request);
    
                                            crm.Execute(request);
                                            crm.Execute(setStateRequest);
    
                                            throw new InvalidPluginExecutionException("Successfully close quoteclose entity");
    
                                            //setStateRequest.State = new OptionSetValue(3);
                                            //setStateRequest.Status = new OptionSetValue(-1);
                                            //crm.Execute(setStateRequest);
                                            //throw new InvalidPluginExecutionException("Success from active to inactive!!!");
                                        }
    
                                    }
                                    crm.SaveChanges();
                                }
                            }
                            else
                            {
                                throw new InvalidPluginExecutionException(" The trigger field was NOT updated");
                            }
    
                        }

    Wednesday, January 2, 2013 11:53 PM
  • Hi,

    Instead of using SetStateRequest to activate your quote, you should use SetStateQuoteRequest for Activating the Quote, but once is active you close it as won using CloseQuoteRequest.

    SetStateQuoteRequest NewSr = new SetStateQuoteRequest();
    NewSr.EntityId = theQuote.quoteid.Value;
    NewSr.QuoteState = QuoteState.Active;
    NewSr.QuoteStatus = 3;
    SetStateQuoteResponse response = new SetStateQuoteResponse();
    response = theService.Execute(NewSr);
    //Quote is active now
    
    quoteclose myquoteclose = new quoteclose();
    myquoteclose.quoteid = new Lookup();
    myquoteclose.quoteid.type = EntityName.quote.ToString();
    myquoteclose.quoteid.Value = YOURQUOTEGUID;
    
    CloseQuoteRequest quotecloserequest = new CloseQuoteRequest();
    quotecloserequest.QuoteClose = myquoteclose;
    quotecloserequest.Status = 5;
    //quote is closed
    
    I use this after I have activated the quote using SetStateQuoteRequest to QuoteState.Active. And I only activate it if the quote is Draft (statuscode.Value == 1). If is not draft, then you can close it directly or it could give an error trying to activate an already active Quote.


    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful" Thanks, Imran Abbasi

    • Marked as answer by valCrmDev2 Thursday, January 3, 2013 4:10 PM
    Thursday, January 3, 2013 1:24 AM
  • Thank you guys for your input. Looking at both examples finally opened up my eyes as to what I needed to do to get this working. I have the desired results now. One more question. I have been firing this state change from a plugin registered on the update of opportunity entity. I have a custom field on the form that, when I update it and save, the plugin fires. Instead of updating the custom field manually and saving in order to fire the plugin, I want to be able to automate this through a custom ribbon button. I have the custom button created, I just wanted to know how to implement the javascript or SOAP to achieve this. Or if its ever possible to force an update as such.
    Thursday, January 3, 2013 4:28 PM