SNOWYCODE
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
How To Add A Hyperlink To A Notification In ServiceNow
How To's

How To Add A Hyperlink To A Notification In ServiceNow

How To Add a Hyperlink to a Notification in ServiceNow

Introduction

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.

Methods for Adding a Hyperlink to Notifications

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.

URI

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}.

ServiceNow URI Notification Subject and Message HTML
Notification Record

ServiceNow URI Notification Preview
Notification Content

URI_REF

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}:

ServiceNow URI_REF Notification Subject and Message HTML
Notification Record

ServiceNow URI_REF Notification Preview
Notification Content

Mail Script

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.

Example 1: Mail Script for Hyperlink to Employee Center

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);

ServiceNow Mail Script Notification Subject and Message HTML
Notification Record

ServiceNow Mail Script Notification Preview
Notification Content

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.

Example 2: Mail Script to Build your own Hyperlink

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);

ServiceNow Hyperlink Notification Subject and Message HTML
Notification Record

ServiceNow Mail Script Notification Preview
Notification Content

Example 3: Mail Script using gs.generateURL

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);

Notification Record

Notification Content

Conclusion

 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!

Snowycode team
Tags:
No tags found
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Read more