Getting data into dynamic content

bhargav ram
Tera Contributor

Hello All,

var gr = new GlideRecord('t_record');
gr.addQuery('sys_created_on','>=',"2024-02-01");
gr.addQuery('sys_created_on','<=',"2024-02-31");
gr.addQuery('producer.sys_name','=',"sample");
gr.query();
var c=0;
while(gr.next()){
c++;
}

In dynamic content block i have done this i need to get 621 records present but i am getting 500 only and if records less than 500 are present then i am getting exact value. If more than 500 records present it directly shows 500 only.

1 ACCEPTED SOLUTION

Martin iTSM
Tera Guru

Hey there,

what does the value of gr.getRowCount()); show after gr.query();?
Did you try to create the same filter in the condition builder in the list view of the table [t_record]?
If you get you 621 records there - copy the query and do an gr.addEncodedQuery('your_copied_query_string_here'); instead of your 3 gr.addQuery(...) lines and see if that changes anything?

If you still get max 500 records maybe there`s a limitation on those dynamic content blocks - but i didn`t find any sys_properties on a first look.

Tried with this script to query the [sys_user] table and got 624 which matches the list count - so it doesn`t look like there`s a limitation:

 

<?xml version="1.0" encoding="utf-8" ?>

<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">

<g:evaluate var="jvar_test" object="false" jelly="true">

        var count=0;

        var gr= new GlideRecord('sys_user');
        gr.query();
		count = gr.getRowCount();
		count;

</g:evaluate>
User count is: ${jvar_test}   
</j:jelly>

 


Cheers!

Martin

View solution in original post

5 REPLIES 5

Martin iTSM
Tera Guru

Hey there,

what does the value of gr.getRowCount()); show after gr.query();?
Did you try to create the same filter in the condition builder in the list view of the table [t_record]?
If you get you 621 records there - copy the query and do an gr.addEncodedQuery('your_copied_query_string_here'); instead of your 3 gr.addQuery(...) lines and see if that changes anything?

If you still get max 500 records maybe there`s a limitation on those dynamic content blocks - but i didn`t find any sys_properties on a first look.

Tried with this script to query the [sys_user] table and got 624 which matches the list count - so it doesn`t look like there`s a limitation:

 

<?xml version="1.0" encoding="utf-8" ?>

<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">

<g:evaluate var="jvar_test" object="false" jelly="true">

        var count=0;

        var gr= new GlideRecord('sys_user');
        gr.query();
		count = gr.getRowCount();
		count;

</g:evaluate>
User count is: ${jvar_test}   
</j:jelly>

 


Cheers!

Martin

Aniket Bhanse
Tera Guru

Have you tried removing all the Query which you have put in the GlideRecord?

var gr = new GlideRecord('t_record');
gr.query();
var abc=0;
while(gr.next()){
abc++;
}

 

This is to identify if there is something to do with the Query. Also, I have updated the variable to be "abc" rather than "c".

 

Let me know if this works.

Community Alums
Not applicable

Hi @bhargav ram ,

 

Please use the below code which I tried using GlideAggregate & it will always return the correct count irrespective of what the query is. 

 

 

var grINC = new GlideAggregate("incident");
grINC.addEncodedQuery("active=true"); //replace it with query of your choice
grINC.addAggregate('COUNT');
grINC.query();
if(grINC.next()){
 var countofINCs =  grINC.getAggregate('COUNT');
  gs.print("Count of Incidents is "+countofINCs);  
}

 

 

As per the ServiceNow best practices we need to use GlideAggregate for getting the count of records instead of getRowCount so please use the below code which I've mentioned & it's working fine for me.

 

Let me know if this code fulfills your requirement or let me know so that I can make necessary changes.

 

Please mark my solution as Accepted if it helps you.

Thanks & Regards,

Madhan Somesh.

 

ServiceNow best practices FYI ServiceNow Scripting Best Practices 

Hi Madhan,

completely agree with your GlideAggregate/Best Pracice approach - for testing and debugging i still tend to use the getRowCount() since it`s just easier than copying the whole GlideAggregate each time - and in this case we`re just working on 600-something records.

But still good and valid point - thanks!

Martin