Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to split String and store in array

Sironi
Kilo Sage

Hi,

 

Could you please help me on below issue.

How to split CN = value only and store in array variable 

 

CN=COBROUTEV2,OU=Servers,OU=Computers,OU=COB,DC=abc,DC=root^CN=BTHORMANVM1,OU=Workstations,DC=abc,DC=root

find_real_file.png

 

i tried below one but not working.

var gr=current.u_member.split(',');
var gg=gr.split('CN=');
gs.addInfoMessage(gs.getMessage("GR:"+gg));

 

it is returning single CN= value, but there are 2 CN values. 

some records had 10 CN values. so how can we do this time.

 

How to fix this issue.

1 ACCEPTED SOLUTION

Try below.

var getinarray=[];
var textis='CN=COBROUTEV2,OU=Servers,OU=Computers,OU=COB,DC=abc,DC=root^CN=BTHORMANVM1,OU=Workstations,DC=abc,DC=root';
textis=textis.replace(/\^/g,','); //replaces ^ with comma
var splitcomma=textis.split(','); //splits by comma
for(var i=0;i<splitcomma.length;i++)
{
if(splitcomma[i].indexOf('CN=')>-1) //if contains CN it will go in if loop
{
getinarray.push(splitcomma[i].split('=')[1]);
}
}
gs.print('Value is '+getinarray);

View solution in original post

13 REPLIES 13

Hi,

 

Thanks for reply.

it is returning single CN= value, but there are 2values, not only 2values, some records had 10 CN values. so how can we do this time.

find_real_file.png

can i expect any updates please

can i expect any updates?plse

can i expect any updates?

Jaspal Singh
Mega Patron
Mega Patron

Hi,

 

Try below in background script once.

var textis='CN=COBROUTEV2,OU=Servers,OU=Computers,OU=COB,DC=abc,DC=root^CN=BTHORMANVM1,OU=Workstations,DC=abc,DC=root';
textis=textis.replace(/\^/g,','); //replaces ^ with comma
var splitcomma=textis.split(','); //splits by comma
for(var i=0;i<splitcomma.length;i++)
{
if(splitcomma[i].indexOf('CN=')>-1) //if contains CN it will go in if loop
{
 gs.print('Value is '+splitcomma[i]);
}
}

 

Output:

find_real_file.png