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

Thank you so much Goran for your perspective. Yes, I do agree that for all the existing records, it should be a one time fix and for the new records it has to be a BR. I'm personally a bit hesitant to do a bulk edit unless and until, that's the option left for me.



Best,


Darshak


Perfect. Code works . It's exatly job onDemand


Thanks


davide_fais
Tera Expert

goranlundqvist



Hi Goran,


can you help my with this another query?


How can i modify your query for calculate first assigment?




find_real_file.png


How do you mean, you want to see how many of the RITM has true as first assignment?


I've done this script, but its not optimized:




var gr1 = new GlideRecord('u_history_task');


gr1.query();


while(gr1.next())


{


var gr2 = new GlideRecord('u_history_task');


gr2.addQuery('u_task',gr1.u_task);


gr2.addQuery('u_assignment_group',gr1.u_assignment_group);


gr2.orderBy('u_assignment_date');


gr2.setLimit(1);


gr2.query();


if(gr2.next())


{


gr2.u_first_assignment='true';


gr2.update();


}



}