- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-20-2018 11:42 PM
Dear all,
I could not find the steps to trigger an event using ScheduleJob.
Could anyone point me out steps by step
regards
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2018 07:27 AM
As per the documentation, the gr is a glide record object. To be honest i'm not 100% sure on its purpose or function in this context.
It's not something you can use and reference in your mail script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-20-2018 11:47 PM
Hi scal,
this might be it,
if (current.operation() != 'insert' && current.comments.changes()) { gs.eventQueue('request.commented', current, gs.getUserID(), gs.getUserName()); }
https://docs.servicenow.com/bundle/london-platform-administration/page/administer/platform-events/task/t_CreateYourOwnEvent.html
Hope this helps
Frank
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2018 12:05 AM
Hi franck, sorry a bit confuse as it new for me this part.
My scenario is as below :
- I have define a business rule in Asset table which set a boolean Field SendEmail= true or False as below :
(function executeRule(current, previous /*null when async*/) {
//If the current record State =Retired and Costcenter field has change
// we then set the flag u_send_record_to_mail to True
//This flag will be used to querry all records which have it set
if(current.state=="7" || current.cost_center.changes()){
current.u_send_record_to_mail=true;
}
else{
// we then set the flag u_send_record_to_mail to False
current.u_send_record_to_mail=false;
}
})(current, previous);
This works accordingly and SendEmail flag is set correctly based on condition.
- Then I have define an Event in EventRegistry named for instance MyEvent
- Then I need to create a ScheduleJob which fire this event MyEvent every 5 minutes
- Then I will need to create an Email notification which will trig from myEvent and send Email out
How can I define the step which create the ScheduleJob to fire myEvent every 5 minutes ? Do I need to add also teh same condition as in my business rule to trig the event in order that it is not trig if nothing need to be done ?
Thanks for helping me
regards
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2018 01:23 AM
Hi Scal,
If you set the scheduled job to run every 5 minutes then it will execute your script every 5 minutes and generate an email for any record that is identified by your script.
So, if you want to send an email for every record where the u_send_record_to_mail field is true you can just add that as a query in your glide record. I'm assuming you don't want to spam the same records with mails every 5 minutes so you should also change that field to false at the same time so it is excluded the next time the scheduled job runs.
var gr = new GlideRecord('your_table_name');
gr.addQuery('u_send_record_to_mail', true);
gr.query();
while(gr.next()){
gs.eventQueue('your.event.name', gr, gr.getValue('assigned_to')); //change assigned_to to whoever the recipient should be
gr.u_send_record_to_mail = false;
gr.update();
}
The question then is: why are you doing this in a scheduled job? Why don't you just trigger the event in the business rule rather than using the business rule to change a custom field to true or false and then running job to search for those records? This would be much simpler:
(function executeRule(current, previous /*null when async*/) {
//If the current record State =Retired and Costcenter field has change
//generate an event to send a notification
if(current.state=="7" || current.cost_center.changes()){
gs.eventQueue('your.event.name', current, current.assigned_to);
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2018 01:39 AM
hi david, thnaks for your reply
For clarification my script in schedule job is as below :
//Build record object on asset table
var gr = new GlideRecord('alm_asset');
//Append the querry to generate the view record based on condition
gr.addEncodedQuery('u_send_record_to_mail=true');
gr.query();
//If tehre is at least one records, we fire the event
while (gr.next()) {
gs.addInfoMessage("Event fired");
gs.eventQueue("Asset.retired.ccchange", gr);
//we reset the flag in order to avoid unneeded resend
gr.u_send_record_to_mail = false;
gr.update();
}
IMPORTANT :
What I need to get from the gr.query is ALL records which have u_send_record_to_mail=true.
So I could have 5 records return for instance.
Then I should collect this all records in order to place them in my email body.
few question based on your reply.
Q1 : Does the gr will contains all record when event is fired ?
Q2 : Once it is fired, I need to set u_send_record_to_mail =false for all return records. how can I do that ?
The reason that I am using a SchedulerJob for fireing the event which will then trig a notification email, is that customer would like to receive on scheduled based email if any records available.
If I do this in Business Rule, the event will be trigger each time a record condition is validated
Am I correct ?