Chuck Tomasi
Tera Patron

Most of us are aware of business rules - those lovely little gems that typically run right before or after a database operation (among other things that I'll talk about later.) Sometimes we want to run a script at a different point in our process, for example, when Discovery is complete. ServiceNow has another type of server side script called a Script Action that allows you to run a script triggered from an event.


find_real_file.png

Hopefully, you are familiar with using events and mail notifications. If not, here's a quick recap: an event is commonly triggered from a business rule or workflow (Generate Event) activity, the corresponding mail notification definition is watching for it, and an email gets sent. Script actions behave in much the same way as mail notifications. However, they run a script of your own design rather than sending a mail notification. Again like notifications, you can use either out-of-the-box events, or register your own.

A recent example I have is from a customer who wanted to run a script at the end of discovery. I looked in the event registry and found an existing event called "discovery.complete". I then went to System Policy> Events> Script Action and wrote my script. Like other scripts in ServiceNow, provide a name, set the active flag to true (checked), and optional condition. The key to Script Actions are that you also specify the event (triggered from a business rule, workflow, etc.) When that event is triggered, the script will run. Script Actions are a very handy tool to keep in your toolbox.

Note: the script area uses a large string field instead of a script field which results in a proportional font, making indenting a little odd.

To debug, consider using the background script or an 'on demand' scheduled job to fire the event. This may not be applicable in all cases, but could help speed up development and testing. For my case, I didn't want to run an entire discovery since the test data was readily available once Discovery populated several CIs for me to test. Normally, Discovery would have taken 10-20 minutes to complete each pass; a lot of overhead just to fire the event. My trick was to go to System Definition> Scripts - Background and enter:



gs.eventQueue('discovery.complete', null, 'parm1', 'parm2');


I could have also gone to System Defintion> Scheduled Jobs and create a script that runs on demand. If you do this, open your scheduled job record and click the "Execute Now" to fire your event with just one click.

2 Comments