How To Show An Info or Error Message On A Form

Informational and Error Messages with g_form() Sometimes, you want to show everyone visiting a certain form some information. Say you’re experiencing an outage, or there’s a message you want to broadcast to everyone using a …

info message on incident client script

Informational and Error Messages with g_form()

Sometimes, you want to show everyone visiting a certain form some information.

Say you’re experiencing an outage, or there’s a message you want to broadcast to everyone using a certain application?

As an IT Org, there will always be situations where you want to broadcast information to your end users, and ServiceNow is a great place to do this.

You can run this simple script on any form and show it to any group of users.

You can use ServiceNow’s g_form class with a Client Script to always show informational text on a form.

Say you want to show this to just a certain group of users? You can also update the script to do this.

Whatever you want to broadcast with an informational or error message, you can easily do it with a small and simple script. This needs to be done on the client, and not on the server, so think client script and not business rules when you’re doing this.

We’ll show you how to add info and error messages on an incident, based on a users roles below.

 

How To Show An Info or Error Message On A Form

Role required: admin

Go to your client scripts and create a new record.

This first script will show the info message to everyone.

Fill out the Client script exactly like the below.

Show Info Message, Always

g_form.addInfoMessage()

Script:

function onLoad() {
g_form.addInfoMessage(“Here is an informational message”);
}

 

Here is what you will get once you save the client script and visit the incident form.

info message on incident form

Show Info Message, Based On Role (ITIL)

This is pretty simple, but say you want to just show this to your ITIL users. So if you have an end user visiting the form, they won’t see the info message.

If you wanted to do that, you’d simply modify the script to only reach the g_form.addInfoMessage() line if after we check the user’s roles.

We’ll use g_user.hasRole(“itil”) to check the users role, before reaching the g_form.addInfoMessage() line

It would look like this:

use g_user to show to itil users

Script:

function onLoad() {
if (g_user.hasRole(“itil”)) {
g_form.addInfoMessage(“Here is an informational message”);
}
}

Go take a look at the form as an ITIL user. You will see the info message.

Then go and impersonate a non rolled end user, you will be able to view an incident – but you won’t see the info message.

So this is how we can control what we show to end users based on their role.

Show Info Message, Based On Role (Non ITIL)

To show the info message to NON itil users, just switch up the script to return false on the If statement. We can actually do this with a single “!” to return true if it is false or falsy.

That would now look like this:

return false on the if statement

Script:

function onLoad() {
if (!g_user.hasRole(“itil”)) {
g_form.addInfoMessage(“Here is an informational message”);
}
}

Show An Error Message

This is a simple syntax switch, you just change the method to addErrorMessage.

show error message

Script:

function onLoad() {
g_form.addErrorMessage(“Here is an informational message”);
}

 Show Error Message, Based On Field Value

Script:

function onLoad() {
var priority = g_form.getValue(“priority”);
if (priority == 1) {
g_form.addErrorMessage(“Here is an informational message”);
}
}

show error message by field value only

 Adding an info message or error message is simple, but has a lot of power.

What have you used g_form.addInfoMessage() or g_form.addErrorMessage() on in your ServiceNow environment?

What issues have you had broadcasting messages to your end users?

Let us know below.



What Do You Think About This Article?

5 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x