Configure notification with scheduled Job

ARAVINDA11
Tera Contributor

@Ankur Bawiskar  @Community Alums  @Sohail Khilji  @Maik Skoddow 

 

Please help me how can I configure Email Notification with scheduled Job

1 ACCEPTED SOLUTION

Sohail Khilji
Kilo Patron
Kilo Patron

Hi @ARAVINDA11 ,

 

Example below :


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:
SohailKhilji_0-1716381288556.gif

 

 
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.
SohailKhilji_1-1716381288528.gif

 

 
 

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.
 
SohailKhilji_2-1716381288529.gif

 

 
var gr = new GlideRecord('kb_knowledge');
gr.addEncodedQuery('workflow_state=published^active=true^valid_toRELATIVEEE@dayofweek@ahead@1');
gr.query();
 
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.
 
SohailKhilji_3-1716381288531.gif

 

 

☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect

View solution in original post

2 REPLIES 2

Sohail Khilji
Kilo Patron
Kilo Patron

Hi @ARAVINDA11 ,

 

Example below :


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:
SohailKhilji_0-1716381288556.gif

 

 
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.
SohailKhilji_1-1716381288528.gif

 

 
 

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.
 
SohailKhilji_2-1716381288529.gif

 

 
var gr = new GlideRecord('kb_knowledge');
gr.addEncodedQuery('workflow_state=published^active=true^valid_toRELATIVEEE@dayofweek@ahead@1');
gr.query();
 
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.
 
SohailKhilji_3-1716381288531.gif

 

 

☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect

Sandeep Rajput
Tera Patron
Tera Patron

@ARAVINDA11 You can trigger an email notification from a scheduled job by applying following steps.

1. Register an event first using the Event registry module. For more information please refer to https://docs.servicenow.com/bundle/washingtondc-platform-administration/page/administer/platform-eve...

2. Create an email notification which will get triggered via an event. Select the event name created in step 1. Fore more information please refer to https://docs.servicenow.com/bundle/washingtondc-platform-administration/page/administer/notification...

3. Create a scheduled job and in the script field trigger the event using

gs.eventQueue('<event name>,current,current.number,gs.getUserName());

https://docs.servicenow.com/bundle/washingtondc-platform-administration/page/administer/reference-pa...

 

Hope this helps.