Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

gr.update() not updating all records

Anubhuti
Tera Expert

Hi All,

I am trying to update the work notes of multiple task records, but only one record is getting updated.

Below is the script I am using:

var gr = new GlideRecord('tablename');
gr.addQuery('parent',current.sys_id);
gr.addEncodedQuery('stateNOT IN3,7');
gr.query();
while(gr.next()) {
gr.work_notes = "comments";
gr.update();
}
}

When running it as background script, I am getting the below mentioned error and when I am removing gr.update() i am not getting any error:

Cannot find function getLocation in object com.glide.script.fencing.ScopedUser@@@@@@
Has anybody experienced similar issue?

1 ACCEPTED SOLUTION

Anubhuti
Tera Expert

Hi All,

 

Thanks for your response.

As only one record was getting updated, at first I stored all the sysid in one array and then updated the work notes in another for loop and its working fine.

var arr =[];
var gr = new GlideRecord('tablename');
gr.addQuery('parent',current.sys_id);
gr.addEncodedQuery('stateNOT IN3,7');
gr.query();
while(gr.next()) {
arr.push(gr.sys_id.toString());
}

for(var i =0; i<arr.length; i++){
var gr = new GlideRecord('tablename');
gr.addQuery('sys_id',arr[i].toString());
gr.query();
if(gr.next()) {
gr.work_notes = "comments";
gr.update();
}
}

View solution in original post

13 REPLIES 13

Aaron Munoz
Tera Guru

Try to replace this line

gr.work_notes = "comments";

with this one

gr.setValue('work_notes', "comments");

Tried the same but still getting the error

ChanakyaMekala
Mega Guru
It has to work, however try to replace the encoded query statement with addQuery('work_notes', 'NOT IN', '3,7') Please let me know whether it had worked or not

This also doesn't work.

Simon Christens
Kilo Sage

You can also try:

 

gr['work_notes'].setJournalEntry('comments');

I just tested this and it works as supposed:

var tasks = [];

var task = new GlideRecord('incident');
task.setLimit(10);
task.query();
while(task.next()){

task['work_notes'].setJournalEntry('tester');
task.update();
  
  tasks.push(task.getDisplayValue());
  
}

gs.print(tasks.toString());