sabell2012
Mega Sage
Mega Sage

 

NOTE: MY POSTINGS REFLECT MY OWN VIEWS AND DO NOT NECESSARILY REPRESENT THE VIEWS OF MY EMPLOYER, ACCENTURE.

 

DIFFICULTY LEVEL:    INTERMEDIATE
Assumes having taken the class SSNF and has good intermediate level of knowledge and/or familiarity with Scripting in ServiceNow.


One of the many topics I cover, when teaching the ServiceNow Scripting Class, is Event Handling. Students are sometimes confused by how this is handled inside of ServiceNow. It is actually pretty straightforward, and I will demonstrate with a simple diagram.

 

First let me give you an example of the "why". As in "why" would you use events in the first place. Events allow you to disconnect separate processes from each other. Suppose a Business Rule (BR) is running and needs to make a call to a Script Include (SI) to update a series of records (which could take 30 seconds or longer). If my BR should directly call the SI to run the code it would hold up the entire process (and I may not particularly need any sort of return from the SI). Rather than hold up the user experience waiting for things to finish I can instead call an event and run the SI in the background which would immediately allow the BR to continue and complete; returning control to the user (who is happy because they didn't have to wait!).

 

So here is the picture I now use in my class:

 

Event Handling in a Picture.png

 

0. I register my new event in the Registry, create my Script Action associated to that event, and if needed my Script Include which could be called by the Script Action. Registering my event tells the Worker to listen for that event, and that it will be expected to do something with it.

 

1. Something executes a gs.eventQueue statement which writes a event record on the queue. BTW, this is not an exhaustive list.

 

2,3,4. The event worker(s), whose job it is to listen for events listed in the Registry, picks up the event and sees if there is a Script Action(s) associated with the registered event.

 

5,6. If a Script Action is found to run then it is executed which in turn may execute my Script Include if I choose.

 

Oh yeah, and events are used for notifications as well, but more on that later. 🙂

 

So, there you have it!

 

Enjoy!

Steven Bell.

 

If you find this article helps you, don't forget to log in and mark it as "Helpful"!

 

sabell2012_0-1699286555222.png

 


Originally published on: 08-30-2015 01:55 PM

I updated the code and brought the article into alignment with my new formatting standard.

2 Comments