Quantcast
Channel: Microsoft Dynamics CRM Forum - Recent Threads
Viewing all articles
Browse latest Browse all 82002

Javascript to auto assign Customer field to Test Account

$
0
0

Hi All,

I am trying to use below javascript to set the customer field with Test Account during OnLoad, but doesn't work. I got this code from online somewhere and tried editing it, as I am not a developer myself. Any help is appreciated.

function GetAccountBasedOnName() {

    var accountname = "Test Account";

    var req = new XMLHttpRequest();

    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/accounts?$select=accountid&$filter=name eq '+accountname'", 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++) {

                    var accountid = results.value[i]["accountid"];

                    var lookupValue = new Array();

                    lookupValue[0] = new Object();

                    lookupValue[0].id = accountid;

                    lookupValue[0].name = accountname;

                    lookupValue[0].entityType = "account";

                    Xrm.Page.getAttribute("customerid").setValue(lookupValue);

                }

            }

            else {

                Xrm.Utility.alertDialog(this.statusText);

            }

        };

        req.send();

    }

}

Thanks,

Praveen.


Viewing all articles
Browse latest Browse all 82002

Latest Images

Trending Articles



Latest Images