Part-5 – Code Conversion to latest Web API (Dynamics 365)

Quote Close Request

Using 2011 endpoint in CRM 2011 to CRM 2016 we can call request to close quote like given below

//--***********************************************************************************************//
//-- Quote Close Request by Using 2011 End Point
//--***********************************************************************************************//

function getClientUrl() {
    if (typeof Xrm.Page.context == "object") {
        clientUrl = Xrm.Page.context.getClientUrl();
    }
    var ServicePath = "/XRMServices/2011/Organization.svc/web";
    console.log(clientUrl + ServicePath);
    return clientUrl + ServicePath;
}

function QuoteCloseRequestBy2011() {
    var quoteId = Xrm.Page.data.entity.getId();
    var requestMain = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">";
    requestMain += "  <s:Body>";
    requestMain += "    <Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">";
    requestMain += "      <request i:type=\"b:WinQuoteRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\" xmlns:b=\"http://schemas.microsoft.com/crm/2011/Contracts\">";
    requestMain += "        <a:Parameters xmlns:c=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">";
    requestMain += "          <a:KeyValuePairOfstringanyType>";
    requestMain += "            <c:key>QuoteClose</c:key>";
    requestMain += "            <c:value i:type=\"a:Entity\">";
    requestMain += "              <a:Attributes>";
    requestMain += "                <a:KeyValuePairOfstringanyType>";
    requestMain += "                  <c:key>quoteid</c:key>";
    requestMain += "                  <c:value i:type=\"a:EntityReference\">";
    requestMain += "                    <a:Id>" + quoteId + "</a:Id>";
    requestMain += "                    <a:LogicalName>quote</a:LogicalName>";
    requestMain += "                    <a:Name i:nil=\"true\" />";
    requestMain += "                  </c:value>";
    requestMain += "                </a:KeyValuePairOfstringanyType>";
    requestMain += "                <a:KeyValuePairOfstringanyType>";
    requestMain += "                  <c:key>subject</c:key>";
    requestMain += "                  <c:value i:type=\"d:string\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\">Won the Quote</c:value>";
    requestMain += "                </a:KeyValuePairOfstringanyType>";
    requestMain += "              </a:Attributes>";
    requestMain += "              <a:EntityState i:nil=\"true\" />";
    requestMain += "              <a:FormattedValues />";
    requestMain += "              <a:Id>00000000-0000-0000-0000-000000000000</a:Id>";
    requestMain += "              <a:LogicalName>quoteclose</a:LogicalName>";
    requestMain += "              <a:RelatedEntities />";
    requestMain += "            </c:value>";
    requestMain += "          </a:KeyValuePairOfstringanyType>";
    requestMain += "          <a:KeyValuePairOfstringanyType>";
    requestMain += "            <c:key>Status</c:key>";
    requestMain += "            <c:value i:type=\"a:OptionSetValue\">";
    requestMain += "              <a:Value>4</a:Value>";
    requestMain += "            </c:value>";
    requestMain += "          </a:KeyValuePairOfstringanyType>";
    requestMain += "        </a:Parameters>";
    requestMain += "        <a:RequestId i:nil=\"true\" />";
    requestMain += "        <a:RequestName>WinQuote</a:RequestName>";
    requestMain += "      </request>";
    requestMain += "    </Execute>";
    requestMain += "  </s:Body>";
    requestMain += "</s:Envelope>";

    var req = new XMLHttpRequest();
    req.open("POST", getClientUrl(), false);
    req.setRequestHeader("Accept", "application/xml, text/xml, */*");
    req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    req.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
    req.send(requestMain);
    console.log(req.status);
    console.log(req.responseText);
    if (req.status === 200) {
    }
}

From 2016 we can use latest web API approach like this

//--***********************************************************************************************//
//-- Quote Close Request by Using Web API
//--***********************************************************************************************//

function QuoteCloseRequestByWebAPI() {
    var quoteId = Xrm.Page.data.entity.getId();
    var isSuccessful = false;
    quoteID = quoteID.replace(/\{|\}/gi, '');;
    var clientURL = Xrm.Page.context.getClientUrl();
    var req = new XMLHttpRequest();
    req.open("POST", encodeURI(clientURL + "/api/data/v8.0/WinQuote"), false);
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.onreadystatechange = function () {
        if (this.readyState == 4 /* complete */
        ) {
            req.onreadystatechange = null;
            if (this.status === 204 || this.status === 200) {
                debugger;
                //Action Executed Successfully
                isSuccessful = true;
                return isSuccessful;
            } else {
                var error = JSON.parse(this.response).error;
                console.log(error.message);
            }
        }
    };

    //Parameters
    var body = JSON.stringify({
        "Status": 4,
        "QuoteClose": {
            "subject": "Won Quote",
            "quoteid@odata.bind": clientURL + "/api/data/v8.0/quotes(" + quoteID + ")"
        }
    }); req.send(body);
    return isSuccessful;
}

Author: M. Fasih Akbar

I have 12+ years experience in software industry mainly with Microsoft Technology stack. I have 7+ years of experience with ASP.Net. I am working with Microsoft Dynamics CRM since last 4+ years. Throughout my software development career, I have been engaged in project analyses, as well as in designing applications and databases. My major professional expertise belongs to object oriented web-based technologies like Dynamics CRM, Asp.Net, C#, JQuery, Angular JS, Knockout, Javascript, VB.Net, and MVC.

Leave a comment