A common task that ServiceNow developers will be asked to perform is configuring email notifications. Developers are often required to include a hyperlink in the content of a notification. If you are new to development, or haven’t worked much with notifications, you may be wondering how to add a hyperlink to a notification in ServiceNow.
The good news is there are several methods to accomplish this, and some of them don’t even involve knowing how to code. Other methods which do involve coding allow developers to have more control over building a hyperlink. Having different ways to approach this gives developers flexibility to build a hyperlink to meet each specific use case.
In this blog, we'll discuss how you can add a hyperlink to a notification in ServiceNow.
Knowing how to work with hyperlinks is an important part of configuring email notifications in ServiceNow. Let’s discuss the various methods as well as look at some examples of how to add a hyperlink to a notification in ServiceNow.
The first method we will discuss is using the ${URI} parameter. URI stands for Uniform Resource Identifier. You can use this parameter to add a hyperlink to specific record within the body of a ServiceNow notification.
To add a hyperlink to the notification, simply type ${URI} in the Message HTML field (found under the What it will contain tab) on the notification record. This method does not require any additional coding besides this.
Using ${URI} will create a hyperlink to the associated record in ServiceNow. For example, if the notification was fired from an incident, the result of using ${URI} is a hyperlink that displays the word LINK and will direct the user to the incident form for that specific record. Let’s demonstrate how to configure a hyperlink using ${URI}.
The next method to add a hyperlink to a notification is using the ${URI_REF} parameter. This is almost identical to the first approach we discussed.
It is configured by typing ${URI_REF} in the Message HTML field on the notification record and will also create a hyperlink to the form for the record it was triggered from.
The main difference between using ${URI_REF} vs ${URI} is how the hyperlink is displayed to the user in the notification content. ${URI_REF} will display the record number instead of the word LINK. This is sometimes the preferred approach because it looks nicer in the notification body.
The following example shows how to configure a hyperlink using ${URI_REF}:
The final method for how to add a hyperlink to a notification in ServiceNow is using a mail script. This method involves coding and is configured using an Email script record, which lives on a different table than the notification record we have been discussing thus far.
Since this approach requires scripting, it allows developers to have much more control over the hyperlink. One advantage to using mail scripts is that, unlike URI or URI_REF, developers can create hyperlinks to records or web pages other than the current one that the notification is triggered from. Mail scripts execute server-side, so scripting can be done using any of the server-side APIs available in ServiceNow.
Once the mail script record is created, it can be easily called from a notification. This is achieved by adding ${mail_script:script_name} to the Message HTML field on the notification record, where the “script name” is the name of the Email script record.
There a several different ways to generate a hyperlink using mail scripts. Developers should review these and choose the appropriate method based on the use case. Let’s review some of these methods.
Our first example of building a hyperlink using a mail script is creating a link to route a user to the Employee Center portal. This may be something you are asked to include in a ServiceNow notification to give users an easy way to navigate to the portal. The same method could be used with any static hyperlink such as an external link to Google. Simply copy and paste the code into the Script field on an email script record.
Email script name: url_employee_center
var url = '<a href="' + gs.getProperty('glide.servlet.uri') + 'esc' + '">Click here to visit Employee Center</a>';
template.print(url);
The above example shows how the hyperlink (the URL and text displayed to the user) is configured in the mail script. We can also see how to call that mail script (in this case url_employee_center) by using ${mail_script:url_employee_center} in the notification.
In the next example we will use a bit more coding to build our own hyperlink. Remember, with mail scripts we have access to the server-side APIs. In this case we will be using GlideRecord on the Change Request table when a notification fires for an Approval record. The use case here is to include a link to the related Change Request record in an Approval notification.
Email script name: url_approval_change_record
var grChg = new GlideRecord("change_request");
grChg.addQuery("sys_id", current.sysapproval);
grChg.query();
if (grChg.next()) {
var url = '<a href="' + gs.getProperty('glide.servlet.uri') + grChg.sys_class_name + '.do?sys_id=' + grChg.sys_id + '">' + "Change Request: " + grChg.number + '</a>';
}
template.print(url);
Our final example is same use case as above. The difference here is we are going to use a GlideSystem method called generateURL. This will achieve the same result as the above script, but provides a nice way to do it using less lines of code.
Email script name: generate_url_approval_change
var chg = gs.generateURL(current.sysapproval.sys_class_name, current.sysapproval);
var url = '<a href="' + gs.getProperty('glide.servlet.uri') + chg + '">' + "Change Request: " + current.sysapproval.number + '</a>';
template.print(url);
In this blog we have covered how to add a hyperlink to a notification in ServiceNow.
You are now ready to start building your own custom hyperlinks for notifications. Go forth and conquer!