ServiceNow Learning 31: Insert multiple values in List type field via script in ServiceNow
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-08-2023 08:33 AM
Hi All,
If we have to put multiple values in list field e.g. putting multiple user records in list type field via script. Then the below script would be really helpful for you.
//final_row[1] is the column in excel where we are getting the userid with new line .
var acc = final_row[1].toString().replace(/\s+/g, ','); //remove spaces from the column and replace space with ","
var acc_split = acc.split(",");
var user_arr = [];
for (var a = 0; a < acc_split.length; a++) {
var user1 = new GlideRecord("sys_user");
user1.addQuery("user_name", acc_split[a]);
user1.query();
if (user1.next())
user_arr.push(user1.sys_id.toString());
}
//Insert in table which has list type field
var grco = new GlideRecord("table_name");\
grco.initiliaze();
grco.<list_field> = user_arr.toString();
grco.insert();
Hope this helps.