
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-08-2017 03:45 PM
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:
thanks,
Best Regards.
Davide.
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
-
Team Development
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-09-2017 06:27 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-08-2017 08:03 PM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-08-2017 11:15 PM
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;
}
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-10-2017 12:52 AM
I can not user current. because this is onDemand script for fill an existing table

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-08-2017 11:24 PM
Thanks very much.
Let try me your code
I'll tell you if it works