Scheduled Job to check records with a date and create a TASK

Casey9
Tera Contributor

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'
};

1 ACCEPTED SOLUTION

Mike Patel
Tera Sage

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();

View solution in original post

6 REPLIES 6

Jaspal Singh
Mega Patron
Mega Patron

Hi Casey,

 

u_last_day_of_duty is a Date field or DateTime field.

Hi Jaspal, 

 

It's a Date field (dd/mm/yyyy): 

find_real_file.png

Cheers!

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'
};

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