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

Thank you again Robbie - just tried this on a single record and it worked like a charm! So, just to confirm, if I want to run this against all records that end with CPU, do I just need to comment out the encQBySN line and the computersGR.setLimit(10) line?

Hi @StewartF,

 

You've got it... remove or comment the following lines:

 

 

- computersGR.addEncodedQuery(encQBySN); - This is applying a filter to limit the records to the specific sys_id

- computersGR.setLimit(10); - This again is also just to limit records

 

 

Don't forget to run this as a Fix Script. This allows you to easily roll back if required.

 

@StewartF - So as to help others in the community and for me to gain recognition of my help, can you please Accept my response as the Solution.

 

Thanks, Robbie

Hi @StewartF,

 

How did you go? Did you see my response?

You've got it... remove or comment out the following lines:

 

- computersGR.addEncodedQuery(encQBySN); - This is applying a filter to limit the records to the specific sys_id

- computersGR.setLimit(10); - This again is also just to limit records

 

 

Don't forget to run this as a Fix Script. This allows you to easily roll back if required.

 

@StewartF - So as to help others in the community and for me to gain recognition of my help, can you please Accept my response as the Solution.

 

Thanks, Robbie

Hi Robbie,

 

It worked perfectly in our Sandbox environment, so put it into an Update Set in our Dev instance and tried running on just 10 records initially in there. It runs, says it's updated 10 records, shows in the rollback list as happening - but isn't actually doing anything? Exactly the same script copy/pasted over, not sure why it's not working.

Hi @StewartF,

 

Ok.. .sounds like an environment-specific configuration that we'll have to get the bottom of.

I'm glad the script runs as expected on the Sandbox - it's been tried and tested this end so that's a great step forward.

 

As we know the script works, and we see the rollback record.... can you open one of the computer or vmware records that should have been updated and check the history of the record. This may show us if something is either overriding or preventing us from the update.

 

Under specific controlled conditions, before calling an update, we leverage the .setWorkflow(false) method to ensure no other business rules are triggered allowing our intended update is executed. This is probably what we need to do.

 

I've updated the script as below including this method call above the computersGR.update() method.

 

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.ssetWorkflow(false);
        //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