locked
Problem with JSON Query CRM 2011 RRS feed

  • Question

  • Hi there. 

    After several hours of troubleshooting, I can't figure this one out.  I am trying to obtain the price list rate for a product based on the price list and the product selected on a form.  The code fires off on the OnChange event of the product lookup.  Everything works fine up until the point that it is supposed to assign the rate to another field in the form.  I just can't figure out how to get that value (called "Amount" in the entity).  If I run the query using another tab in IE, it works fine, and brings the result.  Here is my code:

    function GetRate(context) {

        var oProduct = context.getEventSource().getValue()[0].id;
        var oPricelist = Xrm.Page.getAttribute("ews_pricelistid").getValue()[0].id;

        var pagecontext = Xrm.Page.context;
        var serverUrl = pagecontext.getServerUrl();
        var oDataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc";
        var oDataSelect = oDataPath + "/ProductPriceLevelSet?$filter=PriceLevelId/Id eq guid'" + oPricelist + "' and ProductId/Id eq guid'" + oProduct + "'";

        alert(oDataSelect);

        $.ajax({
            type: "GET",
            contentType: "application/json; charset=utf-8",
            datatype: "json",
            url: oDataSelect,
            beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); },
            success: function (data, textStatus, XmlHttpRequest) {
                alert("Success");
                var oRate1 = data.d;
                var oRate = oRate1.Amount;
                alert(oRate);
                Xrm.Page.getAttribute("new_rate").setAttribute(parseFloat(eval(oRate)));
            },
            error: function (XmlHttpRequest, textStatus, errorThrown) {
                alert('OData Select Failed: ' + oDataSelect);
            }
        });
    }

    Thursday, March 31, 2011 2:32 PM

All replies

  • An amount is a decimal not a float. Try changing that. And use setValue
    Thursday, March 31, 2011 3:04 PM
  • The Amount field is a Money field, which are set as float.  I don't think that is the issue.  Right now, I can't even get the alert to show me the value of the amount in the query.  The script pretty much stops after the alert for Success...

    Thursday, March 31, 2011 4:38 PM
  • I think the way you build your query will return a resultset with one entity. So you will have to look at data.d.results[0].

    https://crmbusiness.wordpress.com/2011/03/09/crm-2011-using-odata-and-json/

    Friday, April 1, 2011 10:05 AM