Running a scheduled job to get countdown between two dates

KSLN SAHITHI
Tera Expert

Hi all, I got to fullfill a requirement where, I need to calculate the countdown for a package on when it expires.
In the HR table , there is a " date" filed called  "10th day from now". we need to find difference between two dates that is today's date and "10th day from now" and display the countdown in "u_days_left_for_expirtaion'" field

I have created a  scheduled job to run daily with script 

 

var gr = new GlideRecord('sn_hr_core_case_workforce_admin');
gr.query();
while(gr.next()) {
var tenthday = gr.getValue('10th_day_from_now');
var gdt = new GlideDateTime();
var countdown= GlideDateTime.subtract(gdt,new GlideDateTime(tenth)).getRoundedDayPart();
gr.setValue('u_days_left_for_expirtaion','countdown');

 

however, the script is not working. The scheduled job is set to run on daily basis. Can someone help me find out where the issue is?

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@KSLN SAHITHI 

try this

1) always wrap your script within function

2) use try catch block to handle exception

updateRecords();

function updateRecords(){
	try{
		var gr = new GlideRecord('sn_hr_core_case_workforce_admin');
		gr.addEncodedQuery('10th_day_from_nowISNOTEMPTY'); // add query if field is not empty
		gr.query();
		while(gr.next()) {
			var tenthday = gr.getValue('10th_day_from_now');
			var gdt = new GlideDateTime();
			var countdown = GlideDateTime.subtract(gdt,new GlideDateTime(tenth)).getRoundedDayPart();
			gr.setValue('u_days_left_for_expirtaion',countdown);
			gr.update();
		}
	}
	catch(ex){
		gs.info(ex);
	}
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

2 REPLIES 2

Eswar Chappa
Mega Sage
Mega Sage

Hi @KSLN SAHITHI  try with below code it works

 

 

var gr = new GlideRecord('sn_hr_core_case_workforce_admin');
gr.query();
while(gr.next()) {
var tenthday = gr.getValue('10th_day_from_now');
var start = new GlideDateTime(tenthday);
var end = new GlideDateTime();
var diff = GlideDateTime.subtract(start, end);
var days = diff.getRoundedDayPart();
var countdown=10-days;
gr.setValue('u_days_left_for_expirtaion','countdown');
gs.info(countdown);

 

 

EswarChappa_0-1694092682239.png

Thanks & Regards,

Eswar Chappa

Mark my answer correct and Helpful if this helps you 😀

Ankur Bawiskar
Tera Patron
Tera Patron

@KSLN SAHITHI 

try this

1) always wrap your script within function

2) use try catch block to handle exception

updateRecords();

function updateRecords(){
	try{
		var gr = new GlideRecord('sn_hr_core_case_workforce_admin');
		gr.addEncodedQuery('10th_day_from_nowISNOTEMPTY'); // add query if field is not empty
		gr.query();
		while(gr.next()) {
			var tenthday = gr.getValue('10th_day_from_now');
			var gdt = new GlideDateTime();
			var countdown = GlideDateTime.subtract(gdt,new GlideDateTime(tenth)).getRoundedDayPart();
			gr.setValue('u_days_left_for_expirtaion',countdown);
			gr.update();
		}
	}
	catch(ex){
		gs.info(ex);
	}
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader