- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2023 02:36 AM
Hi Team,
Here I'm having values in list collector, when I'm trying to update with new values it's getting update with one value.
Here we need to update for multiple values also but for multiple values, the first value is getting update with new value and the other values disappearing.
var count=0;
var gr = new GlideRecord('incident');
gr.addEncodedQuery('sys_id=ea98ba3cdb33c81004a55ce2ca96195a');
gr.query();
while (gr.next()){
var demotype = gr.getDisplayValue('demo_type');
var splitdemotype = [];
var splitdemotype = demotype.split(',');
gs.print(splitdemotype);
for (var i = 0; i<splitdemotype; i++){
if(splitdemotype[i].trim() == 'On Hold'){
gs.print('First loop');
gr.setValue('demo_type','03e31627975de950fea876d11153afda');
}
else if(splitdemotype[i].trim() == 'Externally Targetted'){
gs.print('second loop');
gr.setValue('demo_type','4b5e06dddb1eac107b6afe8b0c961916');
}
else if(splitdemotype[i].trim() == 'External – Hight'){
gs.print('third loop');
gr.setValue('demo_type','4b5e06dddb1eac107b6afe8b0c961916');
}
else if(splitdemotype[i].trim() == 'External Host'){
gs.print('fourth loop');
gr.setValue('demo_type','669752a7975de950fea876d11153afa6');
}
else if(splitdemotype[i].trim() == 'External Ramp'){
gs.print('fifth loop');
gr.setValue('demo_type','669752a7975de950fea876d11153afa6');
}
else if(splitdemotype[i].trim() == 'External Loop'){
gs.print('six loop');
gr.setValue('demo_type','669752a7975de950fea876d11153afa6');
}
gr.update();
count++;
}
}
gs.print('Total demo_type records updated are : ' + count);
Result : Executed First loop,third loop
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2023 02:40 AM
Few things to inform
1) use If and not while as you are querying with sysId hence you will get only 1 record in incident table
2) I have used array
use this and check once
var arr = [];
var gr = new GlideRecord('incident');
gr.addEncodedQuery('sys_id=ea98ba3cdb33c81004a55ce2ca96195a');
gr.query();
if (gr.next()){
var demotype = gr.getDisplayValue('demo_type');
var splitdemotype = demotype.split(',');
gs.print(splitdemotype);
for (var i = 0; i<splitdemotype; i++){
if(splitdemotype[i].trim() == 'On Hold'){
gs.print('First loop');
arr.push('03e31627975de950fea876d11153afda');
}
else if(splitdemotype[i].trim() == 'Externally Targetted'){
gs.print('second loop');
arr.push('4b5e06dddb1eac107b6afe8b0c961916');
}
else if(splitdemotype[i].trim() == 'External – Hight'){
gs.print('third loop');
arr.push('4b5e06dddb1eac107b6afe8b0c961916');
}
else if(splitdemotype[i].trim() == 'External Host'){
gs.print('fourth loop');
arr.push('669752a7975de950fea876d11153afa6');
}
else if(splitdemotype[i].trim() == 'External Ramp'){
gs.print('fifth loop');
arr.push('669752a7975de950fea876d11153afa6');
}
else if(splitdemotype[i].trim() == 'External Loop'){
gs.print('six loop');
arr.push('669752a7975de950fea876d11153afa6');
}
}
gr.demo_type = arr.toString();
gr.update();
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-23-2023 12:40 AM
Please replace
for (var i = 0; i<splitdemotype; i++){
with
for (var i = 0; i<splitdemotype.length; i++){
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2023 02:40 AM
Few things to inform
1) use If and not while as you are querying with sysId hence you will get only 1 record in incident table
2) I have used array
use this and check once
var arr = [];
var gr = new GlideRecord('incident');
gr.addEncodedQuery('sys_id=ea98ba3cdb33c81004a55ce2ca96195a');
gr.query();
if (gr.next()){
var demotype = gr.getDisplayValue('demo_type');
var splitdemotype = demotype.split(',');
gs.print(splitdemotype);
for (var i = 0; i<splitdemotype; i++){
if(splitdemotype[i].trim() == 'On Hold'){
gs.print('First loop');
arr.push('03e31627975de950fea876d11153afda');
}
else if(splitdemotype[i].trim() == 'Externally Targetted'){
gs.print('second loop');
arr.push('4b5e06dddb1eac107b6afe8b0c961916');
}
else if(splitdemotype[i].trim() == 'External – Hight'){
gs.print('third loop');
arr.push('4b5e06dddb1eac107b6afe8b0c961916');
}
else if(splitdemotype[i].trim() == 'External Host'){
gs.print('fourth loop');
arr.push('669752a7975de950fea876d11153afa6');
}
else if(splitdemotype[i].trim() == 'External Ramp'){
gs.print('fifth loop');
arr.push('669752a7975de950fea876d11153afa6');
}
else if(splitdemotype[i].trim() == 'External Loop'){
gs.print('six loop');
arr.push('669752a7975de950fea876d11153afa6');
}
}
gr.demo_type = arr.toString();
gr.update();
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2023 03:21 AM
Hi Ankur,
Thanks for your quick response.
I've tried with the given script, this also updating only one value.
In one of my " demo_type ", we have three values. When I tried with the given script, it got updated only one value instead of updating three values.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2023 03:24 AM
are you sure the value you are getting is exact against what you are comparing?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2023 07:03 AM
Yes @Ankur Bawiskar