convert array to a pipe delimited string

nick135
Tera Expert

Hello everyone,

I have a list collector on a catalog item. When the item is submitted the Values of the field on the RITM/SCTASK appear as "a, b, c". I've been asked to convert this format to "a | b | c". so basically the ask here is to remove the comma and insert a pipe. the script I have so far looks a bit like this

//Type appropriate comment here, and begin script below
var arr = g_form.getValue('compliance');
var list;
console.log(arr);
for(var i = 0; i <= arr.length; i++) {

list += arr[i] + " | ";
console.log(list);
}
g_form.setValue('comliance_test', list);
}

 

the problem I'm having is that the list comes back up undefined, and I also end up with an extra pipe added to the end of the string. any ideas?

1 ACCEPTED SOLUTION

vkachineni
Kilo Sage
Kilo Sage
var list_comma = "abc,def,ghi";
var arr = list_comma.split(",");
var list_pipe = arr.join('|'); //join back with pipe
gs.print(list_comma);
gs.print(list_pipe);

/*
*** Script: abc,def,ghi
*** Script: abc|def|ghi
*/
Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

View solution in original post

7 REPLIES 7

Tested example :

find_real_file.png

Output :

find_real_file.png

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

vkachineni
Kilo Sage
Kilo Sage
var list_comma = "abc,def,ghi";
var arr = list_comma.split(",");
var list_pipe = arr.join('|'); //join back with pipe
gs.print(list_comma);
gs.print(list_pipe);

/*
*** Script: abc,def,ghi
*** Script: abc|def|ghi
*/
Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

This worked, thanks a bunch!