- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2025 01:06 AM
Hello Community,
I want to add prefix zzz infront of retired CI for specific cmdb_ci_server class , as there are same names in cmdb_ci_win_server and linuxserver.
i am getting print of these names which are to be added prefix whcih have specific string in one field po_number.
However the update is not happening.
Saw another thread but no luck
Can you please say what is wrong here i am doing please.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2025 03:40 AM
Hi @Nisha30,
Scripting is case sensitive, so within the while loop, the .next() method needs to be all lower case.
Please use the below script. However, can you clarity one point, before even updating, have you followed my initial advise by specifically targeting one record at first to verify results etc.
As @Ankur Bawiskar has also aked, does the PO Number 'contain' decom or does it only have the string 'decom'? - Please confirm. I've provided some lines that can be commented or un-commented.
var sr = new GlideRecord('cmdb_ci_server');
//sr.addQuery('po_number', 'decom'); // Use this if the PO number has the string value of 'decom'
sr.addQuery('po_number', 'CONTAINS', 'decom'); // USe this if the PO number contains the string
sr.addQuery('sys_class_name', 'cmdb_ci_server'); //only cmdb_ci_server CI
sr.addQuery('sys_id', '1072335fc611227500c0267a21be5dc5'); // temporary line to target one specifc record. This can be recomed once verified the script runs as required.
sr.query();
while (sr.next()) {
gs.print('Server name before update: ' + sr.name);
var dName = 'zzz' + sr.name;
sr.name = dName;
gs.print('Server name after update: ' + sr.name);
sr.setWorkflow(false);
sr.update();
}
To help others (and for me to gain recognition for my efforts), please mark this response correct by clicking on Accept as Solution and/or Kudos.
Thanks, Robbie
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2025 03:27 AM
you said you are trying to use contains then this line should be updated as this
sr.addQuery('po_number', 'LIKE', 'decom');
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
02-12-2025 03:40 AM
Hi @Nisha30,
Scripting is case sensitive, so within the while loop, the .next() method needs to be all lower case.
Please use the below script. However, can you clarity one point, before even updating, have you followed my initial advise by specifically targeting one record at first to verify results etc.
As @Ankur Bawiskar has also aked, does the PO Number 'contain' decom or does it only have the string 'decom'? - Please confirm. I've provided some lines that can be commented or un-commented.
var sr = new GlideRecord('cmdb_ci_server');
//sr.addQuery('po_number', 'decom'); // Use this if the PO number has the string value of 'decom'
sr.addQuery('po_number', 'CONTAINS', 'decom'); // USe this if the PO number contains the string
sr.addQuery('sys_class_name', 'cmdb_ci_server'); //only cmdb_ci_server CI
sr.addQuery('sys_id', '1072335fc611227500c0267a21be5dc5'); // temporary line to target one specifc record. This can be recomed once verified the script runs as required.
sr.query();
while (sr.next()) {
gs.print('Server name before update: ' + sr.name);
var dName = 'zzz' + sr.name;
sr.name = dName;
gs.print('Server name after update: ' + sr.name);
sr.setWorkflow(false);
sr.update();
}
To help others (and for me to gain recognition for my efforts), please mark this response correct by clicking on Accept as Solution and/or Kudos.
Thanks, Robbie
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2025 04:49 AM - edited 02-12-2025 04:53 AM
The string exactly contains value = decom.
Does not do anything
var sr = new GlideRecord('cmdb_ci_server'); sr.addQuery('po_number','decom'); sr.addQuery('sys_class_name','cmdb_ci_server');//only for cmdb_ci_server sr.addQuery('sys_id', '00b2d5e9db89e300668ce1aa4b961940'); sr.query(); while(sr.Next()) { // if(sr.hasNext()) // { // gs.print('records found'); var dName = 'zzz' + sr.name; sr.name = dName; sr.setWorkflow(false); sr.update(); //} } |
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2025 05:07 AM
okay the syntax is small q, i corrected that and it goes inside loop now but update of name is not working 😞
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2025 05:08 AM
try this
var dName = 'zzz' + sr.name.toString();
sr.name = dName.toString();
OR
sr.setValue('name', dName.toString());
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