- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2020 10:28 PM
We have a custom table that holds our user on boarding and off boarding details. One of the fields we have is called 'last day of duty'. This gets populated on a users resignation/contract end date. I'm trying to write a script for a scheduled job that will do the following:
1. Query the table and check records that have a last day of due date populated
2. If the date in this field is 7 days time to create a TASK to our desktop team
3. The TASK should capture information from the same table in the TASK (name, manager, last day of duty, location)
I was able to get it working from a Business Rule however have discovered this isn't practical and would prefer a daily job run to check, so have created the following Script Include. Not sure where I've gone wrong, however any advice would be appreciated
var OffBoardingUtils = Class.create();
OffBoardingUtils.prototype = {
initialize: function() {
},
/*Check check the table and check if last day of duty is populated and if within 7 days*/
createCollectTask : function(request){
var target = new GlideRecord('u_ad_update_request');
target.addQuery('u_last_day_of_duty', '!=', '');
var futureDate = new GlideDate();
futureDate.addDaysLocalTime(7);
var dateDif = gs.dateDiff(futureDate.getDisplayValue(),current.u_last_day_of_duty.getDisplayValue(), true);
if (dateDif > 0) {
}
else{
/* Creates a new TASK for hardware collection from AD Update On/Off Boarding Process */
var fr = new GlideRecord('sc_task');
fr.u_business_service = '009b98b64fc06600a728ab4f0310c73e';
fr.assignment_group = 'f660a03a6ff6210057774447ad3ee47d';
fr.due_date = target.u_last_day_of_duty.getDisplayValue();
fr.short_description = "Hardware Collection - " + target.u_first_name + ' ' + target.u_surname + ' - Last Day of Duty: ' + target.u_last_day_of_duty.getDisplayValue();
fr.description = 'Name: ' + target.u_first_name + " " + target.u_surname + "\n";
fr.description += 'Employee Number: U' + target.u_emp + "\n";
fr.description += 'Location: ' + target.u_location + "\n";
fr.description += 'Last Day of Duty: ' + target.u_last_day_of_duty.getDisplayValue() + "\n";
fr.description += 'Position Title: ' + target.u_racq_position_title + "\n";
fr.description += 'Manager: U' + target.u_manager_id;
if(!JSUtil.nil(target.u_manager_id)){
fr.caller = this.getManagerFromRequest(target.u_manager_id);
fr.description += '\n\nPlease complete all steps listed in KB0013322 - User collection process. \nIMPORTANT: Make sure KB is attached when completed.';
}
fr.insert();
}
},
type: 'OffBoardingUtils'
};
Solved! Go to Solution.
- Labels:
-
On-Call Scheduling

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2020 06:15 PM
Try below script Include and schedule Job
var OffBoardingUtils = Class.create();
OffBoardingUtils.prototype = {
createCollectTask: function(request) {
var Day = gs.daysAgo(-7).split(' ');
var target = new GlideRecord('u_ad_update_request');
target.addQuery('u_last_day_of_duty', '!=', '');
target.addQuery('u_last_day_of_duty', Day[0]);
target.query(); //added
while (target.next()) {
/* Creates a new TASK for hardware collection from AD Update On/Off Boarding Process */
var fr = new GlideRecord('sc_task');
fr.u_business_service = '009b98b64fc06600a728ab4f0310c73e';
fr.assignment_group = 'f660a03a6ff6210057774447ad3ee47d';
fr.due_date = target.u_last_day_of_duty.getDisplayValue();
fr.short_description = "Hardware Collection - " + target.u_first_name + ' ' + target.u_surname + ' - Last Day of Duty: ' + target.u_last_day_of_duty.getDisplayValue();
fr.description = 'Name: ' + target.u_first_name + " " + target.u_surname + "\n";
fr.description += 'Employee Number: U' + target.u_emp + "\n";
fr.description += 'Location: ' + target.u_location + "\n";
fr.description += 'Last Day of Duty: ' + target.u_last_day_of_duty.getDisplayValue() + "\n";
fr.description += 'Position Title: ' + target.u_racq_position_title + "\n";
fr.description += 'Manager: U' + target.u_manager_id;
if (!JSUtil.nil(target.u_manager_id)) {
fr.caller = this.getManagerFromRequest(target.u_manager_id);
fr.description += '\n\nPlease complete all steps listed in KB0013322 - User collection process. \nIMPORTANT: Make sure KB is attached when completed.';
}
fr.insert();
}
},
type: 'OffBoardingUtils'
};
Scheduled Job
Run Once a day
new OffBoardingUtils().createCollectTask();

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2020 11:56 PM
Hi Casey,
u_last_day_of_duty is a Date field or DateTime field.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2020 12:00 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2020 12:10 AM
Can you try below with added logs
var OffBoardingUtils = Class.create();
OffBoardingUtils.prototype = {
initialize: function() {
},
/*Check check the table and check if last day of duty is populated and if within 7 days*/
createCollectTask : function(request){
var target = new GlideRecord('u_ad_update_request');
target.addQuery('u_last_day_of_duty', '!=', '');
target.query();//added
gs.log('Records found ',target.getRowCount());//should print number of records
while(target.next())
{
gs.log('In while loop');
var futureDate = new GlideDate();
futureDate.addDaysLocalTime(7);
var dateDif = gs.dateDiff(futureDate.getDisplayValue(),target.u_last_day_of_duty.getDisplayValue(), true);
//replaced current with target
gs.log('Date difference is ',dateDif);
if (dateDif > 0) {
}
else{
/* Creates a new TASK for hardware collection from AD Update On/Off Boarding Process */
var fr = new GlideRecord('sc_task');
fr.u_business_service = '009b98b64fc06600a728ab4f0310c73e';
fr.assignment_group = 'f660a03a6ff6210057774447ad3ee47d';
fr.due_date = target.u_last_day_of_duty.getDisplayValue();
fr.short_description = "Hardware Collection - " + target.u_first_name + ' ' + target.u_surname + ' - Last Day of Duty: ' + target.u_last_day_of_duty.getDisplayValue();
fr.description = 'Name: ' + target.u_first_name + " " + target.u_surname + "\n";
fr.description += 'Employee Number: U' + target.u_emp + "\n";
fr.description += 'Location: ' + target.u_location + "\n";
fr.description += 'Last Day of Duty: ' + target.u_last_day_of_duty.getDisplayValue() + "\n";
fr.description += 'Position Title: ' + target.u_racq_position_title + "\n";
fr.description += 'Manager: U' + target.u_manager_id;
if(!JSUtil.nil(target.u_manager_id)){
fr.caller = this.getManagerFromRequest(target.u_manager_id);
fr.description += '\n\nPlease complete all steps listed in KB0013322 - User collection process. \nIMPORTANT: Make sure KB is attached when completed.';
}
fr.insert();
}
}
},
type: 'OffBoardingUtils'
};
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2020 05:11 PM
Hi,
Thanks for that - have updated and run the script.
The logs generate the following error:
Log Object
null, undefined, or not an object: object