NOTE: MY POSTINGS REFLECT MY OWN VIEWS AND DO NOT NECESSARILY REPRESENT THE VIEWS OF MY EMPLOYER, ACCENTURE.
DIFFICULTY LEVEL: INTERMEDIATE
Assumes basic knowledge and/or familiarity of Scripting in ServiceNow.
NOTE: I have updated the text of this article to reflect the latest version of ServiceNow. SBell - 9/12/16
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:
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.
So, there you have it!
P.S. Oh yeah, and events are used for notifications as well, but more on that later. :-)
For a list of all of my articles: Community Code Snippets: Articles List to Date
Please Share, Like, Bookmark, Mark Helpful, or Comment this blog if you've found it helpful or insightful.
Also, if you are not already, I would like to encourage you to become a member of our blog!