Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Creating a Quote from an Opportunity through a plugin

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

Hello Experts,

I am trying to create quotes from an opportunity using a plugin. I am already firing the plugin on update of the opportunity to run some code which is working fine. I want to add the quote to create quote from the current opportunity.  I am going through the SDK Samples but not getting anywhere. Can anyone guide me with simple steps to do this.

The requirement is to create multiple quotes under an opportunity and assign it to various users for them to further work on. One opportunity will have multiple quotes based on the product selected.

In the samples I can see "GenerateQuoteFromOpportunityRequest" and "GenerateQuoteFromOpportunityResponse" method which I am unable to get it in my SDK.

*This post is locked for comments

  • Royal King Profile Picture
    Royal King 27,686 on at
    RE: Creating a Quote from an Opportunity through a plugin

    you are firing asynchronous call may be that's the reason you are not receiving the result immediately . change the below line of the code

    Replace : xmlhttp.open("Post", serverUrl, true);  

    To : xmlhttp.open("Post", serverUrl, false);

    If this does not work here is the function that works with out any issue in my environment

    function GenerateQuoteFromOpportunityRequest(opportunityid) {

       var requestMain = ""

       requestMain += "<s:Envelope xmlns:s=\"schemas.xmlsoap.org/.../envelope\">";

       requestMain += "  <s:Body>";

       requestMain += "    <Execute xmlns=\"schemas.microsoft.com/.../Services\" xmlns:i=\"www.w3.org/.../XMLSchema-instance\">";

       requestMain += "      <request i:type=\"b:GenerateQuoteFromOpportunityRequest\" xmlns:a=\"schemas.microsoft.com/.../Contracts\" xmlns:b=\"schemas.microsoft.com/.../Contracts\">";

       requestMain += "        <a:Parameters xmlns:c=\"schemas.datacontract.org/.../System.Collections.Generic\">";

       requestMain += "          <a:KeyValuePairOfstringanyType>";

       requestMain += "            <c:key>OpportunityId</c:key>";

       requestMain += "            <c:value i:type=\"d:guid\" xmlns:d=\"schemas.microsoft.com/.../Serialization\">"+opportunityid+"</c:value>";

       requestMain += "          </a:KeyValuePairOfstringanyType>";

       requestMain += "          <a:KeyValuePairOfstringanyType>";

       requestMain += "            <c:key>ColumnSet</c:key>";

       requestMain += "            <c:value i:type=\"a:ColumnSet\">";

       requestMain += "              <a:AllColumns>true</a:AllColumns>";

       requestMain += "              <a:Columns xmlns:d=\"schemas.microsoft.com/.../Arrays\" />";

       requestMain += "            </c:value>";

       requestMain += "          </a:KeyValuePairOfstringanyType>";

       requestMain += "        </a:Parameters>";

       requestMain += "        <a:RequestId i:nil=\"true\" />";

       requestMain += "        <a:RequestName>GenerateQuoteFromOpportunity</a:RequestName>";

       requestMain += "      </request>";

       requestMain += "    </Execute>";

       requestMain += "  </s:Body>";

       requestMain += "</s:Envelope>";

       var req = new XMLHttpRequest();

       req.open("POST", Xrm.Page.context.getClientUrl() + "/XRMServices/2011/Organization.svc/web", false)    

       req.setRequestHeader("Accept", "application/xml, text/xml, */*");

       req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");

       req.setRequestHeader("SOAPAction", "schemas.microsoft.com/.../Execute&quot;);

       var successCallback = null;

       var errorCallback = null;

       req.onreadystatechange = function () {

           if (req.readyState == 4) {

               if (req.status == 200) {

                   var response = req.responseXML;

                   alert(response);

               }

               else {

                   faultXml=req.responseXML;

                   var errorMessage = "Unknown Error (Unable to parse the fault)";

                   if (typeof faultXml == "object") {

                       try {

                           var bodyNode = faultXml.firstChild.firstChild;                      

                           for (var i = 0; i < bodyNode.childNodes.length; i++) {

                               var node = bodyNode.childNodes[i];                            

                               if ("s:Fault" == node.nodeName) {

                                   for (var j = 0; j < node.childNodes.length; j++) {

                                       var faultStringNode = node.childNodes[j];

                                       if ("faultstring" == faultStringNode.nodeName) {

                                           errorMessage = faultStringNode.text;

                                           break;

                                       }

                                   }

                                   break;

                               }

                           }

                       }

                       catch (e) { };

                   }

                   return new Error(errorMessage);

               }

           }

       };

       req.send(requestMain);

    }

  • shivaram Profile Picture
    shivaram 3,315 on at
    RE: Creating a Quote from an Opportunity through a plugin

    Hi chitra,

    I wrote SOAP code.But In AJAX call "Ready state" and "Status" always giving 1 and 0 Respectively..How can I fix this issue??

    Here I attach my code. Please refer this

    function OpportunitytoQuote() {

    var Request = [];

    Request += "<s:Envelope xmlns:s=\"schemas.xmlsoap.org/.../envelope\">";

    Request += " <s:Body>";

    Request += " <Execute xmlns=\"schemas.microsoft.com/.../Services\" xmlns:i=\"www.w3.org/.../XMLSchema-instance\">";

    Request += " <request i:type=\"b:GenerateQuoteFromOpportunity\" xmlns:a=\"schemas.microsoft.com/.../Contracts\" xmlns:b=\"schemas.microsoft.com/.../Contracts\">";

    Request += " <a:Parameters xmlns:c=\"schemas.datacontract.org/.../System.Collections.Generic\">";

    Request += " <a:KeyValuePairOfstringanyType>";

    Request += " <c:key>OpportunityId</c:key>";

    Request += " <c:value i:type=\"a:EntityReference\">";

    Request += " <a:Id>" + Xrm.Page.data.entity.getId() + "</a:Id>";

    Request += " <a:LogicalName>opportunity</a:LogicalName>";

    Request += " <a:Name i:nil=\"true\" />";

    Request += " </c:value>";

    //Request += " <c:value i:type=\"d:guid\" xmlns:d=\"schemas.microsoft.com/.../Serialization\">8059e120-1515-e111-8e08-1cc1def1353b</c:value>";

    Request += " </a:KeyValuePairOfstringanyType>";

    Request += " <a:KeyValuePairOfstringanyType>";

    Request += " <c:key>ColumnSet</c:key>";

    Request += " <c:value i:type=\"a:ColumnSet\">";

    Request += " <a:AllColumns>true</a:AllColumns>";

    Request += " <a:Columns xmlns:d=\"schemas.microsoft.com/.../Arrays\" />";

    Request += " </c:value>";

    Request += " </a:KeyValuePairOfstringanyType>";

    Request += " </a:Parameters>";

    Request += " <a:RequestId i:nil=\"true\" />";

    Request += " <a:RequestName>GenerateQuoteFromOpportunity</a:RequestName>";

    Request += " </request>";

    Request += " </Execute>";

    Request += " </s:Body>";

    Request += "</s:Envelope>";

    var serverUrl = Xrm.Page.context.getClientUrl() + "/XRMServices/2011/Organization.svc/web";

    var xmlhttp = new XMLHttpRequest();

    xmlhttp.open("Post", serverUrl, true);

    xmlhttp.setRequestHeader("Accept", "application/xml, text/xml, */*");

    xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");

    xmlhttp.setRequestHeader("SOAPAction", "schemas.microsoft.com/.../Execute");

    var successCallBack = null;

    var errorCallBack = null;

    xmlhttp.onreadystatechange = function () {

    if (xmlhttp.readyState == 4) {            //readyState is 1

    if (xmlhttp.status == 200) {                //status is 0

    var Response = xmlhttp.responseXML;

    Xrm.Utility.openEntityForm("quote");

    }

    }

    };

    xmlhttp.send(Request);

    }

    Thanks in advance

  • Royal King Profile Picture
    Royal King 27,686 on at
    RE: Creating a Quote from an Opportunity through a plugin

    if you use javascript to create a Quote from opportunity  using SOAP xml version of the same you can open the newly created record using Xrm.Utility.OpenEntityForm() method

  • shivaram Profile Picture
    shivaram 3,315 on at
    RE: Creating a Quote from an Opportunity through a plugin

    Thank you chitra..Its really useful..Can you please suggest me one thing.? Is there any chance to open Quote directly after creating it.?

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Creating a Quote from an Opportunity through a plugin

    Hello Chitra, Please ignore my message. Got confused with the namespace.

    Microsoft.Crm.Sdk.Messages; has it

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Creating a Quote from an Opportunity through a plugin

    Hello Chitra,

    Thanks for the input. Can you please guide me which namespace I should use to get the methods "GenerateQuoteFromOpportunityRequest" and "GenerateQuoteFromOpportunityResponse". I have used all the name spaces such as Xrm.sdk, xrm.sdk.messages etc still not getting those methods on my IDE

  • Verified answer
    Royal King Profile Picture
    Royal King 27,686 on at
    RE: Creating a Quote from an Opportunity through a plugin

    you can use below method to create quote from opportunity , just need to pass opportunity guid to generate quote.

    GenerateQuoteFromOpportunityRequest req = new GenerateQuoteFromOpportunityRequest();

    req.OpportunityId = new Guid("D911C1BD-23DA-E011-94B4-1CC1DEF177C2");  //replace actual guid

    req.ColumnSet = new ColumnSet(true);

    GenerateQuoteFromOpportunityResponse resp = (GenerateQuoteFromOpportunityResponse)service.Execute(req);

    //use returned quote

    Quote quote = (Quote)resp.Entity;

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Congratulations to the January Top 10 leaders!

Check out the January community rock stars...

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,160 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,962 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans