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

Lets try two things now,

add gs.info("check userid = "+userID); // in line 9 to check in script log statements(In app nav) if you have any value

In line 26. make some chnages:

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

 

Now it should work

Best Regards
Aman Kumar

I checked the script log statements and it is returning a value, however the server script is still not adding the user to the list

Hi @Aeden Frost,

 

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

 

Regards,

Snehangshu Sarkar

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

@Aeden Frost 

In line 10 you are querying the table,
But which record you are trying to update is not mentioned, do you have sys_id of the record which you want to update, that you have to add as well, as in

var gr = new GlideRecord("table_name");

gr.addQuery("sys_id","sys_id_of_the_Record");

gr.query():

while(gr.next()){

// add update statements for retrieving the array and setting the value

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

arr.push(userid);

gr.setValue("field_name",arr);
gr.update()

}

}

Best Regards
Aman Kumar

@Aeden Frost 

Can you confirm if you have checked this?

Best Regards
Aman Kumar