- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-07-2023 04:50 AM
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?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-07-2023 06:29 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-07-2023 06:18 AM - edited 09-07-2023 06:22 AM
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);
Thanks & Regards,
Eswar Chappa
Mark my answer correct and Helpful if this helps you 😀
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-07-2023 06:29 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader