Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Query GlideAggregate

davide_fais
Tera Expert

Hi ServiceNow Community,

I've this problem and I hope someone can help me:

I've this table [u_history_task] and I've need to run a job that calcolate for each record value of columns "u_order" and "u_last".

This is a screen of table with an example of records before and How it should be after run the job:

find_real_file.png

                                                                                                         

thanks,

Best Regards.

Davide.

1 ACCEPTED SOLUTION

Ahh... Now that I after a while look at your pictures I think I get what you after.. sometimes you just stares blind on something 😃



Something like this and it only does 2 server calls.




var count = 1; //Get the counter read




var gr = new GlideRecord('change_task'); //Fetch the records we want to update


gr.orderBy('NAME OF YOUR TASK FIELD');


gr.orderBy('sys_updated_on');


gr.query();



var gr2 = new GlideRecord('change_task'); //Fetch another set of record so we can compare to the "next" record


gr2.orderBy('NAME OF YOUR TASK FIELD');


gr2.orderBy('sys_updated_on');


gr2.query();


gr2.next();//Skip ahead one record so gr2 always is one record ahead of gr



while(gr.next()){


if(!gr2.next()){//If there isn't a next record in gr2, gr is the last one


gr.setValue('u_last',true);//To handle the last record


}


gr.setValue('u_order',count);


//Check if the is is the last one and reset the counter if that is true.


if (gr.getValue('NAME OF YOUR TASK FIELD') != gr2.getValue('NAME OF YOUR TASK FIELD')){


gr.setValue('u_last',true);


count = 1;


}


//If it isn't the last one, add 1 to the counter


else {


count++;


}


gr.autoSysFields(false); //Guess you don't want the sys_updated_on field to be updated when you run your script.


gr.update();


}



//Göran


View solution in original post

14 REPLIES 14

Rama Chandra D
Kilo Guru

Hi David,



What do you mean by calculate? Do you mean, you want to read the values of u_order and u_last for each record and put them in u_history_task table?


Can you elaborate what are you trying to accomplish ?



Darshak


Shishir Srivast
Mega Sage

Hi Davide,



I hope, something like below code should help you. Please check.



var chk = 'true';


var gr2_task = '';


var gr = new GlideRecord('u_history_task');


gr.query();


while(gr.next()) {


var gr_task = gr.task;


if(gr_task == gr2_task)


chk = 'false';


else


chk = 'true';



if(chk == 'true')


{


var gr2 = new GlideRecord('u_history_task');


gr2.addQuery('task', gr.task);


gr2.orderByDesc('u_date');


gr2.query();


var count = gr.getRowCount();


var last = 'x';


gr2_task = gr2.task;


while(gr2.next())


{


current.u_order = count;


if(count == gr.getRowCount())


current.u_last = last;


current.update();


count = count - 1;


}


}


}


I can not user current. because this is onDemand script for fill an existing table


davide_fais
Tera Expert

Thanks very much.


Let try me your code


I'll tell you if it works