locked
OnChange Event - JS - New Web API RRS feed

  • Question

  • Hi

    I have a 1:N relationship between the account entity and our new custom entity. We have a lookup on the form of the account entity and when populated with an account, we would like to auto-populate new account number field on the form.

    We are trying to populate our new custom field named new_accountnumber on our custom entity form when an account is associated with the record. We are trying to use an OnChange event on the account lookup and the new CRM 2016 Web API.

    We are using the following script:

    function getAccountNo() {
           debugger;
     var serverURL = Xrm.Page.context.getClientUrl();
     var Lookup = Xrm.Page.getAttribute("new_clientgroupname").getValue();
        var Guid = Lookup[0].id.substring(1, 37);
       var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.1/accounts?$select=accountnumber&$filter=accountid eq" + Guid + "", true);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
    req.onreadystatechange = function() {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 200) {
                var results = JSON.parse(this.response);
                for (var i = 0; i < results.value.length; i++) {
                     Xrm.Page.getAttribute("new_accountnumber").setValue = result["accountnumber"];
                }
            } else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send();
    }

    For some reason, this script is not working and we are receiving an 'Undefined' error message. Would someone please highlight the error in the script?

    Do we need to add additional web resources / libraries to the form?

    Thanks very much
    Tony



    • Edited by TonySim Monday, February 6, 2017 6:33 AM
    Monday, February 6, 2017 5:44 AM

All replies

  • Hi Tony,

    on success call, the result set name is mentioned wrongly.

    you have assigned the response to the "results" variable and retrieving it from "result" variable, this is the reason you are getting undefined error.

    the "accountnumber" will be stored in the results.value array, just put that results variable in console to know the exact place where your field is displayed.

    Also I assume you have added jquery in the library.

    Regards,

    Rekha J

    


    Regards, Rekha J

    Monday, February 6, 2017 6:35 AM
  • Hi Rekha

    Thanks very much for your response.

    No, I have not added Jquery in the library. How do I go about doing this? Do I need to add a new web resource with latest Jquery?

    I seem to think I should set the value using the following:

    Xrm.Page.getAttribute("new_accountnumber").setValue = results.value[i]["accountnumber"];
    Monday, February 6, 2017 6:47 AM
  • Hi Tony,

    You are using the normal http request call itself, this does not require any jquery file actually.

    just make changes to the reslutset variable as you mentioned above, this will work.

    Regards,

    Rekha J


    Regards, Rekha J

    Monday, February 6, 2017 7:18 AM
  • Hi Rekha

    Thanks for your reply.

    Okay, I only have the web resource with the script below included on the form.

    function getAccountNo() {
           debugger;
    	var serverURL = Xrm.Page.context.getClientUrl();
    	var Lookup = Xrm.Page.getAttribute("new_clientgroupname").getValue();
        var Guid = Lookup[0].id.substring(1, 37);
       var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.1/accounts?$select=accountnumber&$filter=accountid eq" + Guid + "", true);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
    req.onreadystatechange = function() {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 200) {
                var results = JSON.parse(this.response);
                for (var i = 0; i < results.value.length; i++) {
                     Xrm.Page.getAttribute("new_accountnumber").setValue = results.value[i]["accountnumber"];
                }
            } else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send();
    }

    For some reason, my script does not work when included as an OnChange event. I have attempted to debug in the browser and I have noticed the following:





    I'm not sure what I am missing.

    Thanks very much
    Tony

    Monday, February 6, 2017 7:41 AM
  • Use below code:

    function getAccountNo() {
           debugger;
     var serverURL = Xrm.Page.context.getClientUrl();
     var Lookup = Xrm.Page.getAttribute("new_clientgroupname").getValue();
        var Guid = Lookup[0].id.substring(1, 37);
       var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.1/accounts(" + Guid + ")?$select=accountnumber", true);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
    req.onreadystatechange = function() {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 200) {
                var results = JSON.parse(this.response);
    
                     Xrm.Page.getAttribute("new_accountnumber").setValue(result["accountnumber"]);
    
            } else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send();
    }


    Regards, Saad

    • Proposed as answer by Mohd Saad Monday, February 6, 2017 8:24 AM
    Monday, February 6, 2017 8:24 AM
  • Thanks very much Saad, I appear to be a little closer.

    I am now receiving a value for req.onreadystatechange however the 'results' are still undefined.

    Please see these screenshots:

    Thanks for all the help

    Regards
    Tony


    • Edited by TonySim Monday, February 6, 2017 10:03 AM
    Monday, February 6, 2017 8:52 AM
  • Hi

    Just out of interest, I finally have my script working.

    I am now using the following:

    function getAccountNo() {
           debugger;
    	var serverURL = Xrm.Page.context.getClientUrl();
    	var Lookup = Xrm.Page.getAttribute("new_clientgroupname").getValue();
        var Guid = Lookup[0].id.substring(1, 37);
       var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.1/accounts?$select=accountnumber&$filter=accountid eq" + Guid + "", true);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
    req.onreadystatechange = function() {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 200) {
                var results = JSON.parse(this.response);{
    				Xrm.Page.getAttribute("new_accountnumber").setValue(results["accountnumber"]);
                }
    			}
            else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send();
    }

    Thanks
    Anthony


    • Edited by TonySim Monday, February 6, 2017 10:03 AM
    Monday, February 6, 2017 9:53 AM