
- 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 03:37 AM - edited 05-10-2024 06:37 AM
Hi @StewartF,
This is easily achieved using the following script.
Best practice tip - Create this as a 'Fix Script' so it can be reused (if required) but most importantly can be rolled back if required. - See below Screenshot and syntax.
Note - If you need to roll back any of the changes, navigate to the rollbacks by typing 'sys_rollback_context.LIST' into the nav menu.
PLEASE MAKE SURE YOU TEST AND ADAPT THE QUERY IN A LOWER ENVIRONMENT (NON PROD) BEFORE EXECUTING
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 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);
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-10-2024 05:30 AM
Is it possible to amend this see anything with 'CPU' at the end of the name and remove just CPU, rather than removing the last 3 characters and targetting everything, just in case any didn't get that update onto them?
Also is this possible as a BR at all? I've looked at tweaking it to be a BR but I'm not amazing as JS yet
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-10-2024 06:07 AM - edited 05-10-2024 06:08 AM
Hi @StewartF,
It sure is.. however, just to clarify, if it's via a Business Rule, what would trigger the name change? An update to the record? If so, I assume you only want the BR to update that specific record that's being updated... so one computer at a time (rather than the 200+ you mentioned in the original post)
Either way.. see the below screenshots to handle this. Note the highlighted areas.
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 06:34 AM
That doesn't work, doesn't do anything I'm afraid.
The Fix Script, when I ran that initially, it actually renamed every single machine to the one I was querying, so something went badly wrong with that - rolled it back.