måndag 14 mars 2016

Fire an email notification from a schedule job

This question popped up in the community and I thought I might as well put the answer here as well. Question was how you could check if the valid to date is 30 days from now and then send a notification to the KB managers.

First of all, you can find the community thread here: Schedule Job Script to Call Event

Now. The functionality that was wanted: When valid date is 30 days from now, the KB Managers should receive an email notification about it.

This can be solved with a schedule job which runs once a day and triggers an event for each KB article that matches the conditions and finally a notification is trigger of that event.

1. Register the event

First we need to register the event that we are going to put in the queue. This is done by going to the System Policy->Events->Registry.

Here we create a new event that looks like this:

That was pretty easy, so let's head to the list view to get the encoded query.

2. Encoded Query

Quickest way is just to type kb_knowledge.list in the navigator and you get the to the list view of the KB articles. Set up the conditions and when you're done, right click and copy the query.

3. Scheduled Job

Now we need to create the scheduled job which should once every day to see if there is any knowledge articles which is get close to the valid to date. This job goes through the encoded query and for each record it finds that matches the query it will fire of an event so the notifications can be sent. It looks like this and the code is below the picture.

var gr = new GlideRecord('kb_knowledge');
while (gr.next()) {
gs.eventQueue("knowledge.expiring", gr);

As you can see in the gs.eventQueue we only have "gs.eventQueue("knowledge.expiring", gr)" in many cases you also specify parameters 1 & 2 like this: "gs.eventQueue(“incident.commented”, current, gs.getUserID(), gs.getUserName());". But since we don't need those, we just skip them.

4. Notification

Now we only have the notification left. Its pretty, simple. set that it will fire on the event and specify who will recieve it and finally the text for the notification. I'm just showing the section with the "when". The rest is purely case specified, but if there is any questions about it, just post a comment and I'll get back to you.

That's about it.


2 kommentarer:

  1. Thank you, this was useful. Great work Goran!

  2. Been looking for an article like this for a while. Incredibly concise and uses a real world situation as an example. Thanks!

    Would be interested to know if you can pass an object over parm1/parm2