web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Javascript function reference error

(0) ShareShare
ReportReport
Posted on by 161

Hi team,

I have created one function which includes webapi functionality of retrive method

here is the code

function retrieveentityCollection() {
debugger;
var entity="contacts";
var Id = Xrm.Page.data.entity.getId().substring(1, 37);
var options ="?$select=firstname&$filter=_accountid_value eq " + Id + "&$count=true";
var serverURL = Xrm.Page.context.getClientUrl();
var Query = entity + options;
var req = new XMLHttpRequest();
req.open("GET", serverURL + "/api/data/v9.0/" + Query, true);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.onreadystatechange = function() {
if (this.readyState == 4 /* complete */ ) {
req.onreadystatechange = null;
if (this.status == 200) {
var data = JSON.parse(this.response);
if(data['@odata.count']!=null)
Xrm.Page.getAttribute("numberofemployees").setValue(data['@odata.count']);
alert(data['@odata.count']);
for (var i = 0; i < data.value.length; i++) {
var name = data.value[i]["firstname"];
Xrm.Utility.alertDialog(name);
}
else {
var error = JSON.parse(this.response).error;
Xrm.Utility.alertDialog(error.message);
}
}
};
req.send();
}
}

I have refer that function on save of account but i am getting below error and script also i am not able to find out in developer tools source file.

ReferenceError: retrieveentityCollection is not defined
at eval (eval at RunHandlerInternal (wintercrm.crm.dynamics.com/.../ClientApiWrapper.aspx), <anonymous>:1:1)
at RunHandlerInternal (wintercrm.crm.dynamics.com/.../ClientApiWrapper.aspx)
at RunHandlers (wintercrm.crm.dynamics.com/.../ClientApiWrapper.aspx)
at ExecuteHandler (wintercrm.crm.dynamics.com/.../ClientApiWrapper.aspx)
at Mscrm.TurboForm.Control.CustomScriptsManager.$DY_1 (wintercrm.crm.dynamics.com/.../formcontrols.js)
at Mscrm.TurboForm.Control.CustomScriptsManager.executeHandler (wintercrm.crm.dynamics.com/.../formcontrols.js)
at Mscrm.TurboForm.Control.CustomScriptsManager.executeHandlerByDescriptor (wintercrm.crm.dynamics.com/.../formcontrols.js)
at wintercrm.crm.dynamics.com/.../formcontrols.js
at wintercrm.crm.dynamics.com/.../global.ashx
at Mscrm.TurboForm.Control.Data.DataEntity.$El_1 (wintercrm.crm.dynamics.com/.../formcontrols.js)

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Nithya Gopinath Profile Picture
    17,078 on at

    Hi Nagaraj,

    This is because of the incorrect braces in your JS code. Please replace the code as shown below.

    function retrieveentityCollection() {
        debugger;
        var entity = "contacts";
        var Id = Xrm.Page.data.entity.getId().substring(1, 37);
        var options = "?$select=firstname&$filter=_accountid_value eq " + Id + "&$count=true";
        var serverURL = Xrm.Page.context.getClientUrl();
        var Query = entity + options;
        var req = new XMLHttpRequest();
        req.open("GET", serverURL + "/api/data/v9.0/" + Query, true);
        req.setRequestHeader("Accept", "application/json");
        req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        req.setRequestHeader("OData-MaxVersion", "4.0");
        req.setRequestHeader("OData-Version", "4.0");
        req.onreadystatechange = function () {
            if (this.readyState == 4 /* complete */) {
                req.onreadystatechange = null;
                if (this.status == 200) {
                    var data = JSON.parse(this.response);
                    if (data['@odata.count'] != null)
                        Xrm.Page.getAttribute("numberofemployees").setValue(data['@odata.count']);
                    alert(data['@odata.count']);
                    for (var i = 0; i < data.value.length; i++) {
                        var name = data.value[i]["firstname"];
                        Xrm.Utility.alertDialog(name);
                    }
                }
                else {
                    var error = JSON.parse(this.response).error;
                    Xrm.Utility.alertDialog(error.message);
                }
            }
        };
        req.send();
    }

    Hope this helps.

  • Verified answer
    Preeti Sharma Profile Picture
    2,678 on at

    Hi,

    You are missing some braces in your method. Please check as below:

    function retrieveentityCollection() {

       debugger;

       var entity="contacts";

       var Id = Xrm.Page.data.entity.getId().substring(1, 37);

       var options ="?$select=firstname&$filter=_accountid_value eq " + Id + "&$count=true";

       var serverURL = Xrm.Page.context.getClientUrl();

       var Query = entity + options;

       var req = new XMLHttpRequest();

       req.open("GET", serverURL + "/api/data/v9.0/" + Query, true);

       req.setRequestHeader("Accept", "application/json");

       req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

       req.setRequestHeader("OData-MaxVersion", "4.0");

       req.setRequestHeader("OData-Version", "4.0");

       req.onreadystatechange = function() {

           if (this.readyState == 4 /* complete */ ) {

               req.onreadystatechange = null;

               if (this.status == 200) {

                   var data = JSON.parse(this.response);

                   if(data['@odata.count']!=null)

                       Xrm.Page.getAttribute("numberofemployees").setValue(data['@odata.count']);

                   alert(data['@odata.count']);

                   for (var i = 0; i < data.value.length; i++) {

                       var name = data.value[i]["firstname"];

                       Xrm.Utility.alertDialog(name);

                   }

               }

               else {

                   var error = JSON.parse(this.response).error;

                   Xrm.Utility.alertDialog(error.message);

               }

           }

       };

       req.send();

    }

    Thanks:)

  • Nagaraj M Profile Picture
    161 on at

    Thanks Gopinath for your quick response it's highly appreciated,

    i want to implement pagination in this code but i am new to web api can you help me out on this , i am trying it but was not able to find proper methods.

    it's high urgent requirement so need your valuable suggestions and some examples

  • Nagaraj M Profile Picture
    161 on at

    Thanks Preeti for quick reply,

    If you are aware of implementing pagination in retrieve call i need your support to convert this code to make use of pagination ,

    thanks inadvance

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans