The script template that we cover can be used in messages like:
The script itself can be used in:
Quick note: when using the script client side, you'll use the g_form prefix. For example 'g_form.addErrorMessage'
When using the script server side, you'll use the gs. prefix. For example 'gs.addInfoMessage'
Let's break down the two lines in this snippet, ignoring the boilerplate onChange client script code.
Note: if you're new to ServiceNow scripting, the first four lines are auto-generated when you create a client script. So we'll just ignore those in this example.
The first line that we'll focus on is building the link and clickable text, then setting it equal to a variable called link.
In this line, we are simply creating a string that acts as an HTML a href. For a refresher on a href syntax, take a quick look at this.
We set the href equal to the URL and then add the clickable text between the a tags.
The target="_blank" portion will make the link open in a new tab. It's not necessary, but generally a better user experience.
To recap, our href is 'https://www.nba.com/teams' and our clickable text is 'official website here.'
The second line is going to add the actual functionality to display the info message.
It sends the info to the screen by using the familiar "g_form.addInfoMessage" .
The message consists of a string value which is concatenated with the link variable. Together, these allow us to give the user some text, followed by a hyperlinked piece of text.
As mentioned in the beginning of this article, you're not limited to info messages with this script.
Don't forget, the syntax for each is dependent on whether you're using client or server. Here's a quick cheat sheet below:
Client-side (Client Scripts, UI Policies, potentially UI Actions)
Server-side (Business Rules, UI Actions)
You can use this script anytime a hyperlink is needed while developing in ServiceNow. You can also switch it up to use other message types, like addErrorMessage and showFieldMsg. Just swap out the 'addInfoMessage' for whichever one you need, and it's good as new!
That pretty much sums it up for this tutorial. If you need any additional scripting templates, check out our Cheat Sheets and other How-To's below: