
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-10-2024 01:58 AM
How would we go about trimming the last 3 characters off the name string of every single computer record? They all have CPU at the end of them, which we want to strip out completely.
I found a bit of script that I think will help, but what's the best method to do/utilise this?
string.substring(0,string.length-3);
Any help would be greatly appreciated.
FYI - this was done via a Flow a while back, we're just needing to reverse it.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-13-2024 01:53 AM
No worries @StewartF,
Try this... I think we're there now.
To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.
Thanks, Robbie
var encQBySN = "sys_id=79ec46839742c2d032ea7a200153af1a"; //Adjust accordingly for testing. Comment out when happy with the results
var computersCount = 0; //Used for sanity check when testing
var computersNameCPU = 0;
var computersGR = new GlideRecord('cmdb_ci_vmware_instance'); //Change this table as required for specific CI class or type
//computersGR.addEncodedQuery(encQ);
computersGR.addEncodedQuery(encQBySN);
computersGR.setLimit(10); //Adjust this number or comment it out completely to run against all records
computersGR.query();
while(computersGR.next()){
computersCount++;
var computerName = computersGR.getValue('name');
var computerSysID = computersGR.getValue('sys_id');
var endsWith = computerName.endsWith("CPU"); //Note - endWith method is case sensitive so 'cpu' will not match
if(endsWith){ //Only interested in computers ending with CPU
computersNameCPU++;
gs.print('computerName: ' + computerName + ' Sys ID:' + computerSysID); //Comment when running as a bulk action against all Computers
var newComputerName = computerName.substring(0,computerName.length-3);
gs.print('TRIMMED computerName: ' + newComputerName + ' Sys ID:' + computerSysID);
computersGR.setValue('name', newComputerName);
//computersGR.update() //Comment and uncomment as appropriate
}
}
gs.print('Total computers checked: ' + computersCount) //Verify expected reults
gs.print('Total computers ending in CPU: ' + computersCount) //Verify expected reults
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-10-2024 06:40 AM - edited 05-10-2024 06:44 AM
HI @StewartF,
A quick note re the Fix Script - it didn't go badly wrong. Maybe we didn't understand each other - it was designed to do exactly that as per your request (or my understanding of your request).
It is not uncommon to rename a record set, hence my assumption this is what was being asked... Apologies if this caused any issue. The good thing is we covered the rollback.
I've tested the Business Rule on my PDI (Personal Dev Instance) before sharing so I can confirm it works. Can you share the screenshots of the business rule you've implemented please?
A few things to check:
- 'When to Run' Tab - make sure it runs on 'before'
- The condition is set where the Name 'Ends with' and the chosen substring
- The advanced tab is set and you've copied the script as per the 2 screen shots... here's the syntax just in case:
To help others (or for me to help you more directly), 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
05-10-2024 07:01 AM
Hi Robbie,
Ideally I would be wanting to update everything together, but thought having it work in a BR would be a tester.
As I initially mentioned, this was done as part of a flow - would a bit of script in a subsequent flow be workable?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-10-2024 07:11 AM
I'm a little confused buddy.
If you want to update everything (as in all records) in one hit, that's exactly what a Fix Script is for and what the script provided does.
How about you use the fix script but I've made a few adjustment and comments where you can adjust the limits, and updates etc and tune it to what you want before running it on all records...
PS - The business rule looks exactly the same as mine as used on my PDI... to confirm, you definitely have a sample record to play against which ends in CPU and doesn't have any other characters/spaces etc.
Here's the updated script - you''' notice I'm running it against specific Serial numbers. This can be adjusted for sys_id's or whatever unique identifier you wish.
//var encQ = "operational_statusIN1,2,3,4,5" //An encoded query if you want to filter for certain records
var encQBySN = "serial_numberINVVM-370-F60616-KN,DVS-360-J56754-QH"; //Adjust accordingly for testing. Comment out when happy with the results
var count = 0; //Used for sanity check when testing
var computersGR = new GlideRecord('cmdb_ci_computer'); //Change this table as required for specific CI class or type
//computersGR.addEncodedQuery(encQ);
computersGR.addEncodedQuery(encQBySN);
computersGR.setLimit(10); //Adjust this number or comment it out completely to run against all records
computersGR.query();
while(computersGR.next()){
count++;
var computerName = computersGR.getValue('name');
var computerSN = computersGR.getValue('serial_number');
if(computerName.length > 20){
gs.print('computerName: ' + computerName + ' Serial number:' + computerSN); //Comment when running as a bulk action against all Computers
var newComputerName = computerName.substring(0,computerName.length-3);
computersGR.setValue('name', newComputerName);
computersGR.update() //Comment and uncomment as appropriate
}
}
gs.print('Total computers: ' + count) //Verify expected reults
To help others (or for me to help you more directly), 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
05-13-2024 12:19 AM
Hi Robbie,
Trying to run through this morning, and amended slightly based on the table I'm needing to look at and sys_id rather than the SN.
Invalid query string received:null: null
Apologies, I'm clearly missing something here.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-13-2024 01:53 AM
No worries @StewartF,
Try this... I think we're there now.
To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.
Thanks, Robbie
var encQBySN = "sys_id=79ec46839742c2d032ea7a200153af1a"; //Adjust accordingly for testing. Comment out when happy with the results
var computersCount = 0; //Used for sanity check when testing
var computersNameCPU = 0;
var computersGR = new GlideRecord('cmdb_ci_vmware_instance'); //Change this table as required for specific CI class or type
//computersGR.addEncodedQuery(encQ);
computersGR.addEncodedQuery(encQBySN);
computersGR.setLimit(10); //Adjust this number or comment it out completely to run against all records
computersGR.query();
while(computersGR.next()){
computersCount++;
var computerName = computersGR.getValue('name');
var computerSysID = computersGR.getValue('sys_id');
var endsWith = computerName.endsWith("CPU"); //Note - endWith method is case sensitive so 'cpu' will not match
if(endsWith){ //Only interested in computers ending with CPU
computersNameCPU++;
gs.print('computerName: ' + computerName + ' Sys ID:' + computerSysID); //Comment when running as a bulk action against all Computers
var newComputerName = computerName.substring(0,computerName.length-3);
gs.print('TRIMMED computerName: ' + newComputerName + ' Sys ID:' + computerSysID);
computersGR.setValue('name', newComputerName);
//computersGR.update() //Comment and uncomment as appropriate
}
}
gs.print('Total computers checked: ' + computersCount) //Verify expected reults
gs.print('Total computers ending in CPU: ' + computersCount) //Verify expected reults