Triggering an event from a Fix Script

anfield
Tera Guru

I am trying to trigger an event from a fix script which I will eventually convert into a scheduled job. I want to basically trigger the event when there are any new records created on a custom table that day. I have the below script which I can see has fired the event that calls the notification in the logs but I am wondering about how to do a couple of things.

 

Fix Script

var queryString = "sys_created_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()";

var grq = new GlideRecord('u_custom_table');
grq.addEncodedQuery(queryString);
grq.query();

gs.log('CUSTOM TABLE NAME: TABLE CHECK: ROW COUNT IS: ' + grq.getRowCount());
if (grq.getRowCount() > 0) {
grq.eventQueue("Custom Table Notification", grq, null, null);

}

 

1. When I checked the email logs there were hundreds of emails in the logs. I want just one email if there is a new record created that day.

2. Is it possible when I do the above to include fields from the custom table for multiple records from the custom table all into that one notification?

Was going to use a business rule to trigger the event but I think it would end up triggering an event for each record insert, which is not what I want, so I switched to a fix script. Tried gs.eventQueue in the string above, and that did nothing.

Thanks

5 REPLIES 5

anfield
Tera Guru

Now I can see one event in the event logs and it just shows error state. No emails in the email log

Shane J
Tera Guru

Aren't you missing:

grq.query();

if (grq.next()){

gs.log('CUSTOM TABLE NAME: TABLE CHECK: ROW COUNT IS: ' + grq.getRowCount());
if (grq.getRowCount() > 0) {
grq.eventQueue("Custom Table Notification", grq, null, null);

}

}

 

1 - Instead of RowCount why don't you use setLimit('integer') instead?  Then follow that with a if (grq.next()).

 

2 - You'd probably have to create a mail script that your notification would run.  Within there, you do the full GlideRecord query and show whatever you want for each record found.

 

3 - Fix Script is a good alternate to a Background Script, because you can save it etc.  Usually if you need to schedule it, you'd copy it from there then to a new Scheduled Job (run script).

 

If you did a BR, you'd need some kind of flag on the first record saying it had been run, and not to run again if it's found.  I think the Scheduled Job is the best way to go for your goal.

Tried the grq.next. Didnt work, not one event got generated

 

1. Willing to try. Is that a better method than rowcount? Rowcount was intended to show me in the logs that the program was seeing the correct amount of records, and from there id process

Tony Chatfield1
Kilo Patron

Hi, if this script is to be run as scheduled job then assuming it is run once a day you should only trigger 1 sysevent per day.

However you can not run it for a period of 'today' and will need to configrure for last 24 hours or for 'yesterday' otherwise you will not have accurtate results.