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

Announcements

No record found.

News and Announcements icon
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

    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

    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

    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

    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

    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

    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

    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

    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

    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

    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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
11manish Profile Picture

11manish 164

#2
ManoVerse Profile Picture

ManoVerse 147 Super User 2026 Season 1

#3
Zhilan Profile Picture

Zhilan 55

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans