SNOWYCODE
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
How To Add A Hyperlink To An Info Message (addInfoMessage) In ServiceNow
How To's

How To Add A Hyperlink To An Info Message (addInfoMessage) In ServiceNow

How To Add A Hyperlink To An Info Message (addInfoMessage) In ServiceNow

In this article, we'll discuss how you can link to a different page (or internally, like to a knowledge article) from your info message.

Here's an example below, which tells the user to select a valid NBA team and links to the official NBA website.

ServiceNow Catalog Example Of addInfoMessage
Service Catalog example

The script template that we cover can be used in messages like:

  • addInfoMessage
  • addErrorMessage
  • showFieldMsg

The script itself can be used in:

  • Client Scripts
  • Business Rules
  • UI Actions
  • UI Policies (in the script section)

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'

The code snippet

 function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
	
	// build the link and clickable text, set it equal to a variable
	var link = '<a href="https://www.nba.com/teams" target="_blank">official website here.</a>'

	// add the form message, using strings and the link variable that was built above
	g_form.addInfoMessage('Please ensure that you have selected a valid NBA team from the ' + link);
	
}

How it works

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.

Where Else To Use This

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)

  • g_form.addInfoMessage
  • g_form.addErrorMessage
  • g_form.showFieldMsg

Server-side (Business Rules, UI Actions)

  • gs.addInfoMessage
  • gs.addErrorMessage

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:

Interested in making more money as a ServiceNow developer? We're here to help. We're launching a set of courses, 1:1 mentorship and a community to help you increase your earnings.

To learn more, sign up here: Make More Money As A ServiceNow Developer

Snowycode team
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Read more