How to remove final 3 characters from 200+ records 'Name' string

StewartF
Tera Expert

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.

1 ACCEPTED SOLUTION

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

 

View solution in original post

19 REPLIES 19

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.

Community Alums
Not applicable

Hi @StewartF ,

Please try below script 

var name = 'someName';
var t = name.substring(0, name.length -3)

gs.print(t);

SarthakKashyap_0-1715338369222.png

 

Please mark my answer correct and helpful if this works for you

 

Thanks and Regards 

Sarthak

Community Alums
Not applicable

@StewartF ,

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

This doesn't work, doesn't amend anything at all.

Robbie
Kilo Patron
Kilo Patron

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

 

Screenshot 2024-05-10 at 14.03.07.pngScreenshot 2024-05-10 at 14.03.22.png