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

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested answer

Needs to count the case records based on the queue through javascript

(0) ShareShare
ReportReport
Posted on by 35

Hi,

I have wriiten a javascript code to count the number of sctive cases based on the queue(Teams), but am getting undefined error. 

Thanks in advance.

Code written below:

function CountRecordsCCD() {

var recordId = Xrm.Page.data.entity.getId();

var fetchXml = "mapping="logical" distinct="false">
<entity name="incident">
<attribute name="title" />
<attribute name="createdon" />
<attribute name="caseorigincode" />
<attribute name="new_casenumber" />
<attribute name="new_keycustomer" />
<attribute name="incidentid" />
<order attribute="createdon" descending="true" />
<filter type="and">
<condition attribute="new_escalateassignto" operator="eq" uiname="CCD" uitype="team" value'"=+ recordId + "'/>
<condition attribute="statecode" operator="eq" value="0" />
<condition attribute="new_country" operator="eq" value="100000000" />
</filter>
</entity>
</fetch>

var fetch = encodeURI(fetchXml);
var entityname = "incidents";
var serverURL = Xrm.Page.context.getClientUrl();
var Query = entityname + "?fetchXml=" + fetch;
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.0/" + Query, false); //
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.send();
if (req.readyState == 4)
{
req.onreadystatechange = null;
if (200 == req.status) {
var results = JSON.parse(req.response);
var records = results.value[0].recordcount;
Xrm.Page.getAttribute("new_ccd1stweek").setValue(records);
}
else {
console.log(req.statusText);
}
}
}

$$$$$$$$$$$$$$$$$

AM getting the below error, Please help out.

pastedimage1574249291389v1.png

Regards,

Pradeep

I have the same question (0)
  • Suggested answer
    Naveen Ganeshe Profile Picture
    3,397 User Group Leader on at
    RE: Needs to count the case records based on the queue through javascript

    Hello Pradeep,

    It looks like you have invalid XML that is causing the issue. Please see below screenshot:

    pastedimage1574254826585v1.png

    Please try below code. I have made some changes and It should work now:

    function CountRecordsCCD() {
    var recordId = Xrm.Page.data.entity.getId();
    var fetchXml= '' 
    '' 
    '' 
    '' 
    '' 
    '' 
    '' 
    '' 
    '' 
    '' 
    '' 
    '' 
    '' 
    '' 
    '' 
    '';
    var fetch = encodeURI(fetchXml);
    var entityname = "incidents";
    var serverURL = Xrm.Page.context.getClientUrl();
    var Query = entityname   "?fetchXml="   fetch;
    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl()   "/api/data/v9.0/"   Query, false); //
    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.send();
    if (req.readyState == 4) {
    req.onreadystatechange = null;
    if (200 == req.status) {
    var results = JSON.parse(req.response);
    var records = results.value[0].recordcount;
    Xrm.Page.getAttribute("new_ccd1stweek").setValue(records);
    }
    else {
    console.log(req.statusText);
    }
    }
    }

  • Suggested answer
    ajyendra Profile Picture
    1,738 on at
    RE: Needs to count the case records based on the queue through javascript

    Change your code from this and also add your logic when you get response

    function CountRecordsCCD() {

    var fetchXmlQuery = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>"+
    "<entity name='incident'>"+
    "<attribute name='title' />"+
    "<attribute name='createdon' />"+
    "<attribute name='caseorigincode' />"+
    "<attribute name='new_casenumber' />"+
    "<attribute name='new_keycustomer' />"+
    "<attribute name='incidentid' />"+
    "<order attribute='createdon' descending='true' />"+
    "<filter type='and'>"+
    "<condition attribute='new_escalateassignto' operator='eq' uiname='CCD' uitype='team' value='" + recordid + "'/>"+
    "<condition attribute='statecode' operator='eq' value='0' />"+
    "<condition attribute='new_country' operator='eq' value='100000000' />"+
    "</filter>"+
    "</entity>"+
    "</fetch>"

    var req = new XMLHttpRequest();
    req.open(
    "GET",
    Xrm.Page.context.getClientUrl() +
    "/api/data/v9.0/incidents?fetchXml=" +
    encodeURIComponent(fetchXmlQuery),
    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) {
    req.onreadystatechange = null;
    if (this.status === 200) {
    var results = JSON.parse(this.response);
    console.dir(results);
    } else {
    alert(this.statusText);
    }
    }
    };
    req.send();

    }

  • Pradeep Reddy  Profile Picture
    35 on at
    RE: Needs to count the case records based on the queue through javascript

    Hi Naveen,

    Thanks for you kind response.

    I added the above javascript to onload and onchange event, but am getting the below error.

    pastedimage1574333456222v1.png

    Regards,

    Pradeep

  • Pradeep Reddy  Profile Picture
    35 on at
    RE: Needs to count the case records based on the queue through javascript

    Hi Naveen,

    Thanks for you kind response.

    I added the above javascript to onload and onchange event, but am getting the below error.

    3060.pastedimage1574333456222v1.png

    Regards,

    Pradeep

  • Pradeep Reddy  Profile Picture
    35 on at
    RE: Needs to count the case records based on the queue through javascript

    Hi Ajyendra,

    Thanks for your kind response.

    I have added a code which you have suggested, but am getting below error.

    pastedimage1574333826657v1.png

    Regards,

    Pradeep

  • Suggested answer
    Naveen Ganeshe Profile Picture
    3,397 User Group Leader on at
    RE: Needs to count the case records based on the queue through javascript

    Hello Pradeep,

    Please try the below code

    function CountRecordsCCD() {
        var recordId = Xrm.Page.data.entity.getId();
        var fetchXml = ''  
            ''  
            ''  
            ''  
            ''  
            ''  
            ''  
            ''  
            ''  
            ''  
            ''  
            ''  
            ''  
            ''  
            ''  
            '';
        var req = new XMLHttpRequest();
        req.open(
            "GET",
            Xrm.Page.context.getClientUrl()  
            "/api/data/v9.0/incidents?fetchXml="  
            encodeURIComponent(fetchXmlQuery),
            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) {
                req.onreadystatechange = null;
                if (this.status === 200) {
                    var results = JSON.parse(this.response);
                    console.dir(results);
                } else {
                    alert(this.statusText);
                }
            }
        };
        req.send();
    }

  • Pradeep Reddy  Profile Picture
    35 on at
    RE: Needs to count the case records based on the queue through javascript

    Hi Naveen,

    I tried the javascript which you have suggested, but still am getting the below error.

    pastedimage1574396077907v1.png

    Regards,

    Pradeep

  • Pradeep Reddy  Profile Picture
    35 on at
    RE: Needs to count the case records based on the queue through javascript

    Hi Naveen,

    Thanks for your response.

    Requirement is the Count of number of cases is active on particular queue(CCD) and this value should be shown in particular field which i have mentioned.

  • Suggested answer
    Naveen Ganeshe Profile Picture
    3,397 User Group Leader on at
    RE: Needs to count the case records based on the queue through javascript

    Hi Pradeep,

    It was my fault that I forgot to change the variable name of fetchXML. I have updated the code to update the field also. please try again

    function CountRecordsCCD() {
        var recordId = Xrm.Page.data.entity.getId();
        var fetchXml = ''  
            ''  
            ''  
            ''  
            ''  
            ''  
            ''  
            ''  
            ''  
            ''  
            ''  
            ''  
            ''  
            ''  
            ''  
            '';
        var req = new XMLHttpRequest();
        req.open(
            "GET",
            Xrm.Page.context.getClientUrl()  
            "/api/data/v9.0/incidents?fetchXml="  
            encodeURIComponent(fetchXml),
            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) {
                req.onreadystatechange = null;
                if (this.status === 200) {
                    var results = JSON.parse(this.response);
    	            var records = results.value[0].recordcount;
    	            Xrm.Page.getAttribute("new_ccd1stweek").setValue(records);
                    console.dir(results);
                } else {
                    alert(this.statusText);
                }
            }
        };
        req.send();
    }

  • Suggested answer
    ajyendra Profile Picture
    1,738 on at
    RE: Needs to count the case records based on the queue through javascript

    Sorry I forgot to add that Line

    function CountRecordsCCD() {

    var recordId = Xrm.Page.data.entity.getId();

    var fetchXmlQuery = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>"+

    "<entity name='incident'>"+

    "<attribute name='title' />"+

    "<attribute name='createdon' />"+

    "<attribute name='caseorigincode' />"+

    "<attribute name='new_casenumber' />"+

    "<attribute name='new_keycustomer' />"+

    "<attribute name='incidentid' />"+

    "<order attribute='createdon' descending='true' />"+

    "<filter type='and'>"+

    "<condition attribute='new_escalateassignto' operator='eq' uiname='CCD' uitype='team' value='" + recordId + "'/>"+

    "<condition attribute='statecode' operator='eq' value='0' />"+

    "<condition attribute='new_country' operator='eq' value='100000000' />"+

    "</filter>"+

    "</entity>"+

    "</fetch>"

    var req = new XMLHttpRequest();

    req.open(

    "GET",

    Xrm.Page.context.getClientUrl() +

    "/api/data/v9.0/incidents?fetchXml=" +

    encodeURIComponent(fetchXmlQuery),

    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) {

    req.onreadystatechange = null;

    if (this.status === 200) {

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

    var records = results.value[0].recordcount;

    Xrm.Page.getAttribute("new_ccd1stweek").setValue(records);

    } else {

    alert(this.statusText);

    }

    }

    };

    req.send();

    }

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 120 Super User 2025 Season 2

#2
MVP-Daniyal Khaleel Profile Picture

MVP-Daniyal Khaleel 119

#3
Erin Lubben Profile Picture

Erin Lubben 66

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans