
- 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-14-2024 03:07 AM
Hi Robbie,
It helps when my colleague tells me about a Business Rule he had that was still active and re-appending each of the machines haha.
Turned that off, and it's chugging through like a trooper now. Thank you so much for your invaluable help there, will mark the script that worked intially as the solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-10-2024 03:53 AM
Hi @StewartF ,
Please try below script
var name = 'someName';
var t = name.substring(0, name.length -3)
gs.print(t);
Please mark my answer correct and helpful if this works for you
Thanks and Regards
Sarthak
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-10-2024 03:54 AM
You mentioned
string.substring(0,string.length-3);
Try to capture in a variable and try to print that variable
Please mark my answer correct and helpful if this works for you
Thanks and Regards
Sarthak

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-10-2024 05:45 AM
This doesn't work, doesn't amend anything at all.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-10-2024 06:09 AM
Hi @StewartF,
(In case my response is lost in the thread)
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