How to create a ServiceNow popup alert box using a business rule - Evanios

How to create a ServiceNow popup alert box using a business rule


Building a ServiceNow popup alert is certainly possible. In building one of our applications we have a UI action available on the list view.   This action fetches data from the customer environment using the MID server, and will take time to complete.   It was important to let the user know that it will take time for this to complete.

One option is to use gs.addInfoMessage:

gs.addInfoMessage('Update set to Agent, it could take up to 1 minute for the screens to be refreshed");

This is a fairly straightforward, and documented here:  https://wiki.servicenow.com/index.php?title=GlideSystem#addInfoMessage.28Object.29

The result looks like this:

listviewaddinfomessage

 

With a large list view, we felt this wasn’t enough.   The best solution was to use a servicenow popup alert box, but all common logic said this is not possible with a business rule.    However, since HTML can be sent within the gs.addInfoMessage command, why not add a little JavaScript?

Here is our solution to create a pop-up box to be displayed after a business rule runs:

var msg = 'Update sent to Agent, it could take up to 1 minute for the screens to be refreshed';
gs.addInfoMessage('<script type=text/javascript>var t=setTimeout(function(){alert("' + msg + '")},500);</script>' + msg);

First, we have to use the setTimeout function to delay for a set number of milliseconds.   Second, we set the alert message.   The reason the timeout is important is because we wanted the screen to re-draw before showing the pop-up alert box.    In some situations it might be preferable not to use a delay, and just pop up the box immediately with a white background prior to the page being rendered.

The end result looks like this, after the action the page is rendered, then we see the pop-up:

ServiceNow popup alert

Hope someone finds this useful. Happy coding! and good luck with creating servicenow popup alert box with business rule