Rick Forristall
Tera Guru

Displaying List Field Type Values in Email Scripts

beaker-meme-need-list-values-in-email-cropped.jpg

When trying to display values from a List field in an email script you can't simply display the field name from the form.

Here's my specific work example:

I created a List field type called "Sizes Needed" (u_sizes_needed) in a form used to submit a marketing request.

This List type field references a custom table that has a single custom field called size (see Figure 1).

I needed to display in an email to the Marketing department AND to the requester all the sizes the requester selected in the form. NOTE: that when you view a previously-submitted form, ServiceNow automatically converts this to a comma-separate text string. I expected this to happen when I was working in my email script, but what I got was a comma-separate string of sys_ids and not the associated values. So I needed to do a GlideRecord on this custom sizes table then cycle through the values I found to get their associated display name.

Here's how I did it. I use a notification email script because I'm getting and displaying the values in two separate emails — one to the requester and one to the marketing team.

First here's the wrong way:

If (current.u_sizes_needed !== "") {
          template.print("<br />Sizes Needed: " + current.u_sizes_needed);
}

This produces something like this in the email:

Sizes Needed: 597267290f434b00d3a3590be1050e67,597267290f434b00d3a3590be1050322,597267290f434b00d3a3590be1043391

Well that's just not going to work for either the requester or marketing — so here's how I got it to work.

  1. I do a GlideRecord query on the custom table
  2. I use the available "IN" javascript operator that comes with the GlideRecord object (https://docs.servicenow.com/bundle/istanbul-application-development/page/script/server-scripting/con... — scroll down to the Available JavaScript operators section).
  3. I cycle through all the selections that are found, then get the associated field value in that table that I want (I this case I wanted the 'size' field value from the custom table).
  4. I add each to an existing variable.
  5. I use the template.print() method to print the variable's contents into the email body.

if (current.u_sizes_needed !== "") {
        var sizes_to_display = "";        
        var selected_sizes = new GlideRecord('x_goaz_marcom_marketing_communications_request_print_sizes');
        selected_sizes.addQuery('sys_id', 'IN', current.u_sizes_needed);        
        selected_sizes.query();
        while(selected_sizes.next() ) {
                sizes_to_display += " — " + selected_sizes.size;
        }
        template.print("<br /><br />Sizes Needed: " + sizes_to_display); }

So in the email that marketing and the requester receives, if they select size options, they are listed like this:

Sizes Needed:

— Full Size

— Diecut

As I learn more tidbits, hacks, tips and tricks for ServiceNow, I'm posting them on my blog: ServiceNow-101.com.

Version history
Last update:
‎01-22-2018 12:29 PM
Updated by: