Scheduled job not working properly

castle11
Tera Expert

Hi, we have a field on our incidents called 'release date'. This is supposed to be filled in with a date in the future when a ticket is put 'on hold'. The ticket should then automatically come off hold on that date. I have setup a scheduled job to do this, however it's taking them off hold exactly 1 day later than the release date. For example, if I set the release date as tomorrow (10th of October), the ticket will not come off hold until the 11th. Here are the job details. Is there anything obvious that I'm missing? The script that it's executing is working, as I said the tickets are coming off hold at 5am, but for some reason 1 day late.

scheduled job.PNG

 

1 ACCEPTED SOLUTION

Hi @castle11 
Just check the output of this script in you Background Script :- 

gs.info((new Date()));
If the time zone shown here is different, I assume that might be causing an issue 
Can you try using GlideDateTime instead of new Date().
You can use this script. Check if it completely matches your use case.
The only case where this can fail(might and might not and depends on your scope) is, when you are using scoped application, subtract  might not work and you should transfer it to a script include present in Global scope

 

 

 

var inc = new GlideRecord('incident');
// Get incidents with 'On Hold' state
inc.addQuery('state', 3);
inc.query();

var today = new GlideDateTime().getDate();
gs.log("Scheduled job running: Check on hold incidents");

while(inc.next()) {
        // Convert the release date to GlideDateTime object then subtract the dates
	var releaseDate =  new GlideDateTime(inc.u_on_hold_release_date).getDate();
        var dur = new GlideDuration();
        dur = GlideDateTime.subtract(releaseDate, today);
        var difference = dur.getDayPart();

	// Check if the difference between the dates has 0 days 
	if(difference == 0) {
		inc.u_on_hold_release_date = '';
		// Set state as 'In Progress'
		inc.state = 2;
		inc.update();
	}
}

 

 

 

 

View solution in original post

6 REPLIES 6

Ashutosh C J
Tera Guru

@castle11 
Try to verify the Time zone of Run as user

AshutoshCJogl_0-1696873314801.png

 

Thanks for your reply. The time zone is set correctly. I can see in the log that the job is executing every morning at 5am which is correct. The script that it's executing is just looking up incidents with release date set as current date and then taking them off hold. But for some reason, it's not taking them off hold until exactly 24 hours later than the release date. So if I set it as tomorrow (11th October), it will take it off hold on the 12th. Makes no sense. Unless I've made a mistake in the script somewhere

var inc = new GlideRecord('incident');
// Get incidents with 'On Hold' state
inc.addQuery('state', 3);
inc.query();

var today = formatDate(new Date());
gs.log("Scheduled job running: Check on hold incidents");

while(inc.next()) {
	var releaseDate = inc.u_on_hold_release_date;
	// Check if the date in the 'Release date' field <= todays date
	if(releaseDate == today) {
		inc.u_on_hold_release_date = '';
		// Set state as 'In Progress'
		inc.state = 2;
		inc.update();
	}
}

// Format date as yyyy-mm-dd
function formatDate(date) {
	var day = date.getDate();
	// Month + 1 as getMonth() starts from 0, so January is 0 for example
	var month = date.getMonth() + 1;
	var year = date.getFullYear();
	// Prefix the day with a zero if it's less than 10 as this isn't automatic
	if (day < 10) {
		day = '0' + day;
	}
	// Prefix the month with a zero if it's less than 10 as this isn't automatic
	if (month < 10) {
		month = '0' + month;
	}
	return year + '-' + month + '-' + day;
}

Hi @castle11 
Just check the output of this script in you Background Script :- 

gs.info((new Date()));
If the time zone shown here is different, I assume that might be causing an issue 
Can you try using GlideDateTime instead of new Date().
You can use this script. Check if it completely matches your use case.
The only case where this can fail(might and might not and depends on your scope) is, when you are using scoped application, subtract  might not work and you should transfer it to a script include present in Global scope

 

 

 

var inc = new GlideRecord('incident');
// Get incidents with 'On Hold' state
inc.addQuery('state', 3);
inc.query();

var today = new GlideDateTime().getDate();
gs.log("Scheduled job running: Check on hold incidents");

while(inc.next()) {
        // Convert the release date to GlideDateTime object then subtract the dates
	var releaseDate =  new GlideDateTime(inc.u_on_hold_release_date).getDate();
        var dur = new GlideDuration();
        dur = GlideDateTime.subtract(releaseDate, today);
        var difference = dur.getDayPart();

	// Check if the difference between the dates has 0 days 
	if(difference == 0) {
		inc.u_on_hold_release_date = '';
		// Set state as 'In Progress'
		inc.state = 2;
		inc.update();
	}
}

 

 

 

 

Good point, I checked gs.info((new Date())); and it is in fact a different time zone. I have changed it to your script and will set a ticket to come off hold tomorrow. Will let you know how it goes

 

Thanks!