locked
CRM 2011: Error 400 bad request RRS feed

  • Question

  • Hi all,

    I have tried the below code to display the equivalent price list name while changing the currency in the opportunity form, so I have attached the below libraries to the opportunity form:

    CurrencyOnChange.js

     

    /// <reference path="XrmPageTemplate.js" />
    
    function Function1() 
    {
        var currencyid = Xrm.Page.getAttribute("transactioncurrencyid").getValue()[0].id;
    
        var filter = "$select=Name,PriceLevelId&$filter=TransactionCurrencyId eq " + currencyid;
    
        SDK.JScriptRESTDataOperations.RetrieveMultiple("PriceLevel", filter, Function2, function (error) { alert(error.message); });
    }
    
    function Function2(returnedPriceLists) 
    {
        var pricelists = returnedPriceLists;
    
        for (var pl in pricelists) 
        {
            alert(pricelists[pl].Name);
            alert(pricelists[pl].PriceLevelId);
        }
    }
    

     


    SDK.JScriptRESTDataOperations.js

     

    if (typeof (SDK) == "undefined")
    { SDK = { __namespace: true }; }
    SDK.JScriptRESTDataOperations = {
        _context: function () {
            if (typeof GetGlobalContext != "undefined")
            { return GetGlobalContext(); }
            else {
                if (typeof Xrm != "undefined") {
                    return Xrm.Page.context;
                }
                else
                { return new Error("Context is not available."); }
            }
        },
        _getServerUrl: function () {
            var serverUrl = this._context().getServerUrl()
            if (serverUrl.match(/\/$/)) {
                serverUrl = serverUrl.substring(0, serverUrl.length - 1);
            }
            return serverUrl;
        },
        _ODataPath: function () {
            return this._getServerUrl() + "/XRMServices/2011/OrganizationData.svc/";
        },
        _errorHandler: function (req) {
            return new Error("Error : " +
      req.status + ": " +
      req.statusText + ": " +
      JSON.parse(req.responseText).error.message.value);
        },
        _dateReviver: function (key, value) {
            var a;
            if (typeof value === 'string') {
                a = /Date\(([-+]?\d+)\)/.exec(value);
                if (a) {
                    return new Date(parseInt(value.replace("/Date(", "").replace(")/", ""), 10));
                }
            }
            return value;
        },
        Create: function (object, type, successCallback, errorCallback) {
            var req = new XMLHttpRequest();
            req.open("POST", this._ODataPath() + type + "Set", true);
            req.setRequestHeader("Accept", "application/json");
            req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
            req.onreadystatechange = function () {
                if (this.readyState == 4 /* complete */) {
                    if (this.status == 201) {
                        successCallback(JSON.parse(this.responseText, SDK.JScriptRESTDataOperations._dateReviver).d);
                    }
                    else {
                        errorCallback(SDK.JScriptRESTDataOperations._errorHandler(this));
                    }
                }
            };
            req.send(JSON.stringify(object));
        },
        Retrieve: function (id, type, successCallback, errorCallback) {
            var req = new XMLHttpRequest();
            req.open("GET", this._ODataPath() + type + "Set(guid'" + id + "')", true);
            req.setRequestHeader("Accept", "application/json");
            req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
            req.onreadystatechange = function () {
                if (this.readyState == 4 /* complete */) {
                    if (this.status == 200) {
                        successCallback(JSON.parse(this.responseText, SDK.JScriptRESTDataOperations._dateReviver).d);
                    }
                    else {
                        errorCallback(SDK.JScriptRESTDataOperations._errorHandler(this));
                    }
                }
            };
            req.send();
        },
        Update: function (id, object, type, successCallback, errorCallback) {
            var req = new XMLHttpRequest();
    
            req.open("POST", this._ODataPath() + type + "Set(guid'" + id + "')", true);
            req.setRequestHeader("Accept", "application/json");
            req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
            req.setRequestHeader("X-HTTP-Method", "MERGE");
            req.onreadystatechange = function () {
                if (this.readyState == 4 /* complete */) {
                    if (this.status == 204 || this.status == 1223) {
                        successCallback();
                    }
                    else {
                        errorCallback(SDK.JScriptRESTDataOperations._errorHandler(this));
                    }
                }
            };
            req.send(JSON.stringify(object));
        },
        Delete: function (id, type, successCallback, errorCallback) {
            var req = new XMLHttpRequest();
            req.open("POST", this._ODataPath() + type + "Set(guid'" + id + "')", true);
            req.setRequestHeader("Accept", "application/json");
            req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
            req.setRequestHeader("X-HTTP-Method", "DELETE");
            req.onreadystatechange = function () {
                if (this.readyState == 4 /* complete */) {
                    if (this.status == 204 || this.status == 1223) {
                        successCallback();
                    }
                    else {
                        errorCallback(SDK.JScriptRESTDataOperations._errorHandler(this));
                    }
                }
            };
            req.send();
    
        },
        RetrieveMultiple: function (type, filter, successCallback, errorCallback) {
    
            if (filter != null) {
                filter = "?" + filter;
            }
            else
            { filter = ""; }
    
            var req = new XMLHttpRequest();
            req.open("GET", this._ODataPath() + type + "Set" + filter, true);
            req.setRequestHeader("Accept", "application/json");
            req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
            req.onreadystatechange = function () {
                if (this.readyState == 4 /* complete */) {
                    if (this.status == 200) {
                        successCallback(JSON.parse(this.responseText, SDK.JScriptRESTDataOperations._dateReviver).d.results);
                    }
                    else {
                        errorCallback(SDK.JScriptRESTDataOperations._errorHandler(this));
                    }
                }
    
            };
            req.send();
    
    
        },
        __namespace: true
    };
    

     

    then I referred to the CurrencyOnChange.js in onChange of the currency and used Function1

    But it keep saying: Error 400 bad request: Syntax errror '{' at position 25.

     

    Please advise..

    Sunday, September 25, 2011 1:00 PM

Answers

  • Alert the currencyid variable to see if it contains the curly brackets, {}. If it does use the javascript replace function to remove them like this: currencyid.replace('{', '').

    I'm not sure but in your filter variable you should set the guid like this: ....eq '" + currencyid +"';  Note the single quotes surrounding the guid.

     

    Sunday, September 25, 2011 2:14 PM

All replies