locked
CRM 2011 - On change of the Existing Product on the Quote Product, set the Unit of Measure (UoM) and Price Per Unit [JavaScript] [AJAX] RRS feed

  • Question

  • I've tweaked the code here (http://social.microsoft.com/Forums/en-US/840fe399-7c52-4b92-a371-88aa62dfc4c2/use-javascript-to-update-price-per-unit-when-selecting-a-product-in-dynamics-crm-2011?forum=crmdevelopment) to look at the Quote Product instead of the Opportunity Product. This is for an On-Prem environment, UR14.

    I'm getting an error. "Error:'$' is undefined"

    /////////////////////////////////////////////////////////////////////////////////////////////////
    function updateProductPrice() {
    
        //Developed by PowerObjects 11/9/2012; Modified by ASI 
        //This jscript is intended to fire off on the OnChange event of the Product lookup on Quote Products
        //Once user inputs a product, the default unit for the product will set, and price per unit, from the Price List Item, will populate below
        //This javascript assumes there are price lists.
    
        var productField = Xrm.Page.getAttribute('productid').getValue();
        var productFieldId = productField[0].id;
        if (productFieldId != null) {
    
            //build the query to get the default unit
    
            var pagecontext = Xrm.Page.context;
            var serverUrl = pagecontext.getServerUrl();
            var oDataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc/";
            var oDataSelect = oDataPath + "ProductSet?$select=DefaultUoMId&$filter=ProductId eq guid'" + productFieldId + "'";
    
            //alert(oDataSelect );
    
            //Start AJAX call to get default unit guid from product
    
            $.ajax({
                type: "GET",
                contentType: "application/json; charset=utf-8",
                datatype: "json",
                url: oDataSelect,
                beforeSend: function (XMLHttpRequest) {
                    XMLHttpRequest.setRequestHeader("Accept", "application/json");
                },
                success: function (data, status) {
                    NowHaveDefaultUnitGuid(data, status, productFieldId, oDataPath);
                },
                error: function (XmlHttpRequest, textStatus, errorThrown) {
                    alert("OData Select Failed: " + errorThrown + ". There is no default unit associated with product");
                }
            });
        }
    }
    
    /////////////////////////////////////////////////////////////////////////////////////////////////
    function NowHaveDefaultUnitGuid(data, status, productFieldId, oDataPath) {
    
        //we are passing the oDataPath and productFieldId since we will still need these variables
    
    /*
    
    
    This is the bit I tweaked. Essentially changed all instances of Opportunity to Quote. 
    
    
    */
    
        var defaultUnitGUID = data.d.results[0].DefaultUoMId; //retrieved defaultUnit guid
        var quoteId = Xrm.Page.getAttribute('quoteid').getValue();
        if (quoteId != null) {
            var oDataSelect = oDataPath + "QuoteSet?$select=PriceLevelId&$filter=QuoteId eq guid'" + quoteId + "'";
    
            //start AJAX call for the parent price list guid
            $.ajax({
                type: "GET",
                contentType: "application/json; charset=utf-8",
                datatype: "json",
                url: oDataSelect,
                beforeSend: function (XMLHttpRequest) {
                    XMLHttpRequest.setRequestHeader("Accept", "application/json");
                },
                success: function (data, status) {
                    NowWeHaveEverything(data, status, productFieldId, defaultUnitGUID, oDataPath);
                },
                error: function (XmlHttpRequest, textStatus, errorThrown) {
                    alert('OData Select Failed: ' + errorThrown + ". There is no parent price list");
                }
            });
        }
    }
    
    /////////////////////////////////////////////////////////////////////////////////////////////////
    function NowWeHaveEverything(data, status, productFieldId, defaultUnitGUID, oDataPath) {
    
        //now that we have the pricelist guid, default unit guid, we can lookup the price list item to pull the amount value
    
        var priceLevelId = data.d.results[0].PriceLevelId.Id; //retrieved pricelevel
    
        //start AJAX clal for Price List Item entities = Price List GUID and Product Guid and Unit Guid and return the Amount on that price list item. This will return one results.
    
        var oDataSelect2 = oDataPath + "ProductPriceLevelSet?$select=Amount&$filter=(PriceLevelId/Id eq guid'" + priceLevelId + "' and ProductId/Id eq guid'" + productFieldId +
            "' and UoMId/Id eq guid'" + defaultUnitGUID.Id + "')";
        $.ajax({
            type: "GET",
            contentType: "application/json; charset=utf-8",
            datatype: "json",
            url: oDataSelect2,
            beforeSend: function (XMLHttpRequest) {
                XMLHttpRequest.setRequestHeader("Accept", "application/json");
            },
            success: function (data, status) {
                havePrice(data, defaultUnitGUID);
            },
            error: function (XmlHttpRequest, textStatus, errorThrown) {
                alert('OData Select Failed: ' + errorThrown + ". There is no price list item for this product");
            }
        });
    }
    
    /////////////////////////////////////////////////////////////////////////////////////////////////
    function havePrice(data, defaultUnitGUID) {
        if (defaultUnitGUID != null) {
    
            //set the price per unit
    
            var priceListItemAmount = data.d.results[0].Amount.Value;
            Xrm.Page.getAttribute("priceperunit").setValue(parseFloat(eval(priceListItemAmount)));
            Xrm.Page.getAttribute("priceperunit").setSubmitModeAlways;
            //set the unit on Quote below.
            var id = defaultUnitGUID.id;
            var name = defaultUnitGUID.Name.toString();
            var entityType = defaultUnitGUID.LogicalName.toString();
            Xrm.Page.getAttribute("uomid").setValue([{
                    id: id,
                    name: name,
                    entityType: entityType
                }
            ]);
        }
    }
    ///////////////////////////////////////////////////////////////////////////////////////////////// 


    • Edited by M. Jaxon Wednesday, November 6, 2013 11:11 PM
    Wednesday, November 6, 2013 11:06 PM

Answers

  • Please download latest J-Query library from here and create a JS web resource and paste J-Query library code and attach that web resource as well in your Form.

    http://stackoverflow.com/questions/5168471/crm-2011-is-undefined


    Hope this helps. If you get answer of your question, please mark the response as an answer and vote as helpful !
    Vikram !


    • Edited by _Vikram Thursday, November 7, 2013 3:01 AM
    • Marked as answer by M. Jaxon Thursday, November 7, 2013 4:08 PM
    Thursday, November 7, 2013 2:57 AM

All replies

  • Please download latest J-Query library from here and create a JS web resource and paste J-Query library code and attach that web resource as well in your Form.

    http://stackoverflow.com/questions/5168471/crm-2011-is-undefined


    Hope this helps. If you get answer of your question, please mark the response as an answer and vote as helpful !
    Vikram !


    • Edited by _Vikram Thursday, November 7, 2013 3:01 AM
    • Marked as answer by M. Jaxon Thursday, November 7, 2013 4:08 PM
    Thursday, November 7, 2013 2:57 AM
  • Many thanks!
    Thursday, November 7, 2013 4:09 PM
  • Hi _Vikram,

    Now that the jQuery library is attached to the form, it is working but only on change of the Existing Product field. But when I save the form, I get the following. My tweaks were only pointing the code (in ^original post) to the Quote instead of the Opportunity; I don't think I messed with the unit id so...not sure what to do.

    Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: The unit id is missing.Detail: 
    <OrganizationServiceFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/xrm/2011/Contracts">
      <ErrorCode>-2147206387</ErrorCode>
      <ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic" />
      <Message>The unit id is missing.</Message>
      <Timestamp>2013-11-07T16:26:17.8411299Z</Timestamp>
      <InnerFault i:nil="true" />
      <TraceText i:nil="true" />
    </OrganizationServiceFault>
    

    Thursday, November 7, 2013 4:35 PM