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 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

Hi @Aeden Frost ,

 

I've observed two things here - 

1. change line 28, gr.setValue('u_impacted_users',arr.join(','));

2. change line number 26 => arr = gr.getValue("u_impacted_users").toString().split(',');

 

Regards,

Snehangshu Sarkar

Please mark my answer as correct if it resolves your query.

Hi Snehangshu,

 

I changed the lines that you have reccomended but it still unfortunately is not working.

find_real_file.png

Hi @Aeden Frost,

 

Remove gs.setValue(arr) from your script.

Use the below script. It's tested, should work.

if(action == 'resolve'){

  var arr = [];
  arr = gr.getValue('u_impacted_users').toString().split(',');
  arr.push(<userId>); // if you have any user to add it, replace <userId> with the user sys_id
  gr.setValue('u_impacted_users',arr);
  gr.update();
}

 

Regards,

Snehangshu Sarkar

Please mark my answer as correct if it resolves your query.

Thank you very much for your response, unfortunately it doesn't work. I am trying to add user names to a glide_list if that helps narrow what I am trying to do down.

 

find_real_file.png

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Aeden,

Is u_impacted_users a comma delimited string? If so, change the following lines. 

Change the following lines

var arr = [];
arr = gr.getValue('u_impacted_users');
arr.push(userID);
gr.setValue(arr);
gr.update();

To

arr = gr.getValue('u_impacted_users');
gr.setValue('u_impacted_users', arr + ',' + userID);
gr.update();