Search code examples
javascriptodatadynamics-crmcrm

CRM Odata and Javascript


I am making my first CRM project - it's autoupdate field from some other entity. I read a little and gave it a try. I have:

var Code = Xrm.Page.getAttribute("new_codeid").getValue();
var oDataPath = Xrm.Page.context.getServerUrl() + "/xrmservices/2011/organizationdata.svc" ;

var Query = "/new_codesSet?" +
    "$select=new_city" + 
    "&$filter=new_code eq '" + Code + "'" + 
    "&$top=1";

var Record_Request = new XMLHttpRequest();
Record_Request.open("GET", oDataPath + Query, true);    
Record_Request.setRequestHeader("Accept", "application/json");
Record_Request.setRequestHeader("Content-Type", "application/json; charset=utf-8");
Record_Request.onreadystatechange = function () 
{
    var Value = "";
    if (this.readyState == 4) 
    {
        if (this.status == 200) 
        {
            var RecordSet = JSON.parse(Record_Request.responseText).d;
            if(RecordSet.results.length > 0)
            {
                Value = RecordSet.new_city;
        }       
    }
}; 
Xrm.Page.getAttribute(address1_city).setValue(Value);

I get Unknown Error. How can I debug it? What I am doing wrong?


Solution

  • Best way to debug when using IE is to set your browser to allow script debugging, Options > Advanced > un-tick 'disable script debugging'.

    in your Jscript use debugger; to breakpoint your code, that'll allow you to jump into your code using visual studio for example.

    btw > just check in your second go you are using a variable called PostCodeId.Replace and assigning it to CodeId, what is PostCodeId's value and are you attempting to overwrite CodeId with it?

    I've taken the liberty of writing your code as follow:

    var CodeId = Xrm.Page.getAttribute("new_codeid").getValue();
    debugger; //inseting a breakpoint as explained.
    if (CodeId != null) {
        var serverURL = Xrm.Page.context.getClientUrl();
        var oDataSelect = serverURL +
                "/xrmservices/2011/OrganizationData.svc/new_codeSet?" +
                "$select=new_city" +
                "&$filter=new_code/Id eq guid'" + CodeId[0].id + "'" +
                "&$top=1"; //make sure this is correct by testing this build url in you browser
    
        $.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) {
                ReturnCity(data); ;
    
            },
            error: function (XmlHttpRequest, textStatus, errorObject) {
                alert("OData Execution Error Occurred");
            }
        });
    
        function ReturnCity(data) {
            if (data.d != null) {                 
                var value = data.d.results[0].new_city;
                Xrm.Page.getAttribute("address1_city").setValue(value);
            }
        };
    }
    

    let me know how it goes.