I am not often engaged in non-integration activities, but occasionally, implementation requirements get pushed into integration projects that require that I branch out a bit to parts of the product that are less known to me. I have always known how to do email notifications in ServiceNow, but I have never had to dive into Mail Scripts until now. Many thanks to Ben Yukich for his “mail script” know-how that pointed me in the right direction.

In the following example, any time a new user record is created in ServiceNow, we want to send a personalized email to the user telling them that they have an account with ServiceNow and provide them their username and a one-time password.

In order to make this happen, we will assume that the method for this will be through inbound email actions. If a user sends an email to a ServiceNow instance, and that person does not have a user account, the system will automatically create the account for that user. In this case, there is a system property that defines the password for all new users created in this way.

All of the heavy lifting here will be done within an Email Notification record.

Name: New User Account
Table: User [sys_user]
Send when: Record inserted or updated
Inserted: true
Updated: false
Conditions: Email is not empty
Subject: Welcome to ServiceNow!
Message:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Dear ${first_name} ${last_name},

A user record has been created for you within ServiceNow.  

Please follow these instructions for logging in to the system.

<mail_script>
  template.print("1) Browse to https://" + gs.getProperty("instance_name") + ".service-now.com\n");
  template.print("2) Enter your credentials\n");
  template.print("User ID: "+current.user_name+"\n");
  template.print("Password: "+gs.getProperty("glide.user.default_password")+"\n");

  //Set the email address to the user record that was being created
  email.addAddress("to", current.email, current.first_name + " " + current.last_name);
</mail_script>

This notification will take the instance name from the system property: “instance_name” to build out a URL that the user can click on to log in.

The notification will also grab the email property (glide.user.default_password) that defines the default password that is used when the email system creates the new user record.

The user should receive an email that looks like the following message:

Welcome_to_ServiceNow__-_arbonboy_gmail.com_-_Gmail