How to add users to a list with Server Script?

Aeden Frost
Tera Contributor

Hi Everyone,

 

I am trying to add some functionality which adds users to a list through the service portal, however in the Server Script I had used gr.setValue() 

Unfortunately this deletes all other entries to the list and replaces it with the one user, what would be the best way to add users without removing previous users?

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Aeden Frost 

update as this

line 26

arr = gr.u_impacted_users.toString().split(',');

line 28

gr.setValue('u_impacted_users', arr.toString());

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

19 REPLIES 19

Aman Kumar S
Kilo Patron
What I can suggest, before you use setValue, store the existing value of list in an array. Then add your new value to the array using push, then use setValue and pass the array . var arr = []; arr = gr.getValue("list_field"); arr.push(new_user_sysid); gr.setValue(arr);
Best Regards
Aman Kumar

I attempted to do what you had suggested however it doesn't appear to work, have I made a silly mistake?

find_real_file.png

do not you getUserdisplay, you want sys_id of the user in 3rd line that you have written.

I am assuming you are also passing the sys_Id of the user to be added to list from portal as well

Best Regards
Aman Kumar

I've attempted it with the userID, retrieving it in line 8, however it still does not appear to be working

find_real_file.png