Setting emails in GlideList fields with a script

If you try to populate a GlideList field using a script, you can end up with different values in your table, depending on the way you do it.

GlideList fields such as the Watch list and Work notes list can store 2 types of values:

  • Users (sys_ids of Users in the sys_user table)
  • Emails

Usually, you will want to save references(sys_ids) for users in the system and emails for external users or distribution lists.

Let’s see it with a simple example with only two users.

  • Abel Tuter → Using his email as User ID
  • Abraham Lincoln → Not using his email as User ID

Option 1: Match emails to UserIDs

If we update the GlideRecord using “gr.field = value”, Servicenow will try to match emails to UserIDs in the sys_user table. It will replace the values by the sys_ids if found.

var grIncident = new GlideRecord('incident'); grIncident.get('<sys_id>'); grIncident.watch_list = 'abel.tuter@example.com,abraham.lincoln@example.com'; grIncident.update();
Code language: JavaScript (javascript)

Option 2: Save the email address

If we use “setValue” instead, the email address is stored in the GlideList even if a user with that UserID exists in the system.

var grIncident = new GlideRecord('incident'); grIncident.get('<sys_id>'); grIncident.setValue('watch_list','abel.tuter@example.com,abraham.lincoln@example.com'); grIncident.update();
Code language: JavaScript (javascript)