SEND AN NOTIFICATION USING SCHEDULE JOB

DEXTER RG
Tera Contributor

Notification Timesheet not submitted to employee

 

As an employee, if i have not submitted my timesheet for the previous week before Monday at 2pm AEST

I should receive a notification requesting me to do so.

THANKYOU

1 ACCEPTED SOLUTION

Hi Dexter,

I don't see "user" defined anywhere, it looks like you're trying to use it as a GlideRecord, however it's just a field on the Time Sheet Exceptions table. If you want to call that field you need to use the syntax timecard.user, not just user. Does the Notification run on the sys_user or time_sheet_exception table?

It looks like you have it set up to run on the User table. I would rewrite it this way then. I don't recommend nesting GlideRecords, so we'll build an array of users and then loop through that array to trigger the events. Also I'm going to use an Immediately Invoked Function Expression, instead of defining the function and then calling it. That's just extra steps. Also, use getUniqueValue() function to get sys_id's whenever possible, as you can get some funky behavior otherwise. I always convert sys_id's to strings just out of superstition because of the things I've encountered over the years lol.

I wasn't able to test this of course, but I think this should be pointing you in the right direction. Happy to assist further if needed.

(function () {
var userArr = [];
var timecard = new GlideRecord('time_sheet_exception');
timecard.addQuery('state','Not_Submitted');
timecard.addQuery('week_starts_on<=javascript:gs.daysAgoStart(7)');
timecard.query();

while(timecard.next()){
userArr.push(timecard.user.toString());
}

var user = new GlideRecord('sys_user');
for(var u=0; u < userArr.length; u++){
user.get(userArr[u]);
gs.eventQueue('time_card.late_submit', user, user.getUniqueValue(), user.manager);
gs.log('users' + user.getUniqueValue(), 'timecard');
}

})();

View solution in original post

10 REPLIES 10

Raghu Loganatha
Kilo Guru

If you are a developer then you should be able to create a scheduled job and a notification. 

  1. Create a new event
  2. Create a new notification
  3. Set trigger notification by an event and select the event you created above
  4. Create a scheduled job and write your script to check if you have submitted your timesheet or not and run it every Monday.
  5. If you haven't submitted then trigger the event and that will trigger the notification.

Good luck!!

sachin_namjoshi
Kilo Patron
Kilo Patron

You don't need to write code for achieving this functionality.

You can achieve this using script-less scheduled jobs.

Please refer below for more details

https://community.servicenow.com/community?id=community_blog&sys_id=ae9caee1dbd0dbc01dcaf3231f96193d

 

Regards,

Sachin

DEXTER RG
Tera Contributor

Thanks raghunath..

i tried that...but this script is not working i think

find_real_file.png

Hi Dexter,

I don't see "user" defined anywhere, it looks like you're trying to use it as a GlideRecord, however it's just a field on the Time Sheet Exceptions table. If you want to call that field you need to use the syntax timecard.user, not just user. Does the Notification run on the sys_user or time_sheet_exception table?

It looks like you have it set up to run on the User table. I would rewrite it this way then. I don't recommend nesting GlideRecords, so we'll build an array of users and then loop through that array to trigger the events. Also I'm going to use an Immediately Invoked Function Expression, instead of defining the function and then calling it. That's just extra steps. Also, use getUniqueValue() function to get sys_id's whenever possible, as you can get some funky behavior otherwise. I always convert sys_id's to strings just out of superstition because of the things I've encountered over the years lol.

I wasn't able to test this of course, but I think this should be pointing you in the right direction. Happy to assist further if needed.

(function () {
var userArr = [];
var timecard = new GlideRecord('time_sheet_exception');
timecard.addQuery('state','Not_Submitted');
timecard.addQuery('week_starts_on<=javascript:gs.daysAgoStart(7)');
timecard.query();

while(timecard.next()){
userArr.push(timecard.user.toString());
}

var user = new GlideRecord('sys_user');
for(var u=0; u < userArr.length; u++){
user.get(userArr[u]);
gs.eventQueue('time_card.late_submit', user, user.getUniqueValue(), user.manager);
gs.log('users' + user.getUniqueValue(), 'timecard');
}

})();