Using split functionality to get the name

DB1
Tera Contributor

Hi All,

 

Requirement: To split and get the name of the CI 

Example: Name of the CI: "Test - DHL App - Dev"

The req is to split the 2nd "-" and get the output as "Test - DHL App"

How to achieve the same?

 

var grBusServ = new GlideRecord('cmdb_ci_service');
grBusServ.query();
while(grBusServ.next()){
//gs.print(grBusServ.getRowCount());
gs.print(grBusServ.name);
var name = grBusServ.name;
var finalname = name.indexOf('-');
gs.print('finalname' +finalname);
}

 

TIA

@Ankur Bawiskar @Mark Roethof @Ravi Chandra_K @Lan 

1 ACCEPTED SOLUTION

Robbie
Kilo Patron
Kilo Patron

Hi @DB1,

 

Did you have a chance to review my response?

Did you implement the function?

 

Here the function used with your coding and syntax:

var grBusServ = new GlideRecord('cmdb_ci_service');
grBusServ.addQuery('name', 'Test - DHL App - Dev'); //This query line is for testing. Comment the full line to loop through record set when happy.
grBusServ.query();
while(grBusServ.next()){
//gs.print(grBusServ.getRowCount());
gs.print('name before substring: ' + grBusServ.name);
var name = grBusServ.name;
var secondOccurance = getIndex(name, "-", 2);
var finalname = name.substring(0,secondOccurance)
//Please be sure to use the .trim() function to remove white spaces at the end (and at the start)
gs.print('finalname: ' + finalname.trim());
}

function getIndex(str, character, n) {
    return str.split(character, n).join(character).length;
}

 

 

So as to help others in the community, please mark my answer as correct and/or helpful.

 

Thanks,

Robbie

View solution in original post

2 REPLIES 2

Robbie
Kilo Patron
Kilo Patron

Hi DB1,

 

Try using the below handy little function to find the 'nth' occurrence (in your case the 2nd occurrence) of the character you wish to split by. 

 

function getIndex(str, character, n) {
    return str.split(character, n).join(character).length;
}

//Example code to show how this function is called and used:
var ciFullName = "Test - DHL App - Dev";
var secondOccurance = getIndex(ciFullName, "-", 2);
var ciSubString = ciFullName.substring(0,secondOccurance)
//Please be sure to use the .trim() function to remove white spaces at the end (and at the start)
gs.print(ciSubString.trim());

 

Please also note to use the .trim() function to remove white spaces at the end (and the start) so as to avoid any naming and importantly searching issues with the 'finalname'.

 

To help others, please mark this as correct and/or helpful.

 

Thanks,

Robbie

Robbie
Kilo Patron
Kilo Patron

Hi @DB1,

 

Did you have a chance to review my response?

Did you implement the function?

 

Here the function used with your coding and syntax:

var grBusServ = new GlideRecord('cmdb_ci_service');
grBusServ.addQuery('name', 'Test - DHL App - Dev'); //This query line is for testing. Comment the full line to loop through record set when happy.
grBusServ.query();
while(grBusServ.next()){
//gs.print(grBusServ.getRowCount());
gs.print('name before substring: ' + grBusServ.name);
var name = grBusServ.name;
var secondOccurance = getIndex(name, "-", 2);
var finalname = name.substring(0,secondOccurance)
//Please be sure to use the .trim() function to remove white spaces at the end (and at the start)
gs.print('finalname: ' + finalname.trim());
}

function getIndex(str, character, n) {
    return str.split(character, n).join(character).length;
}

 

 

So as to help others in the community, please mark my answer as correct and/or helpful.

 

Thanks,

Robbie