- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-08-2022 11:40 PM
I have a scheduled job below which will send email every 5, 10,15.....years. Here we want to implement leap year too.
Like user joined on 29th feb 2012 which was leap year and he should receive 5 years completion email on 29th feb 2017 but since 2017 was not leap year, hence there was no 29th feb 2017 and no email trigger. Now, we are in 2022 which is not a leap year and email should trigger to almost 70+ employee on 29th feb which will not get trigger as per code below.
We need to trigger email on next available date which is 1st march 2022. How can we adjust below code to calculate leap year too?
***********
function autoEmail() {
var i;
for (i = 5; i < 40; i= i+5) {
var gdt = new GlideDateTime();
gdt.addYearsUTC(-i);
var queryTime = gdt.getDate();
var gr = new GlideRecord('sn_hr_core_profile');
gr.addQuery('user.u_business_unit', 'US');
gr.addQuery('u_joining_date', queryTime);
gr.query();
while(gr.next()) {
if (gr.user.email.toString() == ""){
gs.eventQueue('sn_hr_core.completion_milestone',gr,i,gr.user.manager.toString());
}else{
gs.eventQueue('sn_hr_core.completion_milestone',gr,i,gr.user.toString());
}
gs.info("Employee : "+ gr.user.name + " completed year "+i);
}}}
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-23-2022 03:47 AM
Hi,
There was small mistake in code, please try below,
function autoEmail() {
var i;
for (i = 5; i < 40; i= i+5) {
var gdt_today=new GlideTime();
gdt_today.addDaysUTC(-1);
var currentMonth=gdt_today.getMonthLocalTime();
var currentDay=gdt_today.getDayOfMonthLocalTime();
var gdt = new GlideDateTime();
gdt.addYearsUTC(-i);
gdt.addDaysUTC(-1);
var month = gdt.getMonthLocalTime();
var date = gdt.getDayOfMonthLocalTime();
if(month==2 && date==29){
if(!(currentMonth==2 && currentDay==29)){
queryTime = gdt.getDate();
}
}
else{
gdt.addDaysUTC(1);
queryTime = gdt.getDate();
}
var gr = new GlideRecord('sn_hr_core_profile');
gr.addQuery('user.u_business_unit', 'US');
gr.addQuery('u_joining_date', queryTime);
gr.query();
while(gr.next()) {
if (gr.user.email.toString() == ""){
gs.eventQueue('sn_hr_core.completion_milestone',gr,i,gr.user.manager.toString());
}else{
gs.eventQueue('sn_hr_core.completion_milestone',gr,i,gr.user.toString());
}
gs.info("Employee : "+ gr.user.name + " completed year "+i);
}}}
Please mark this as Correct or Helpful if it helps.
Thanks and Regards,
Abhijit
Regards,
Abhijit
ServiceNow MVP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-20-2022 11:36 PM
will it send 10 yrs completion email on 1st march if employee joining date was on 29th march 2012? Because in 2022 29th feb doesn't exists
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-21-2022 01:19 AM
Hi,
It will send it on 28th Feb 2022.
Let me know if you have any further queries.
Please mark this as Correct or Helpful if it helps.
Thanks and Regards
Abhijit
Regards,
Abhijit
ServiceNow MVP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-21-2022 06:13 AM
Hi,
I have modified script based on your new question. You can use below script, it will send an email on 1st march if employee joining date is on 29th Feb(only for leap year), for other dates it works as usual.
function autoEmail() {
var i;
for (i = 5; i < 40; i= i+5) {
var gdt = new GlideDateTime();
gdt.addYearsUTC(-i);
gdt.addDaysUTC(-1);
var month = gdt.getMonthLocalTime();
var date = gdt.getDayOfMonthLocalTime();
if(month==2 && date==29){
queryTime = gdt.getDate();
}
else{
gdt.addDaysUTC(1);
queryTime = gdt.getDate();
}
var gr = new GlideRecord('sn_hr_core_profile');
gr.addQuery('user.u_business_unit', 'US');
gr.addQuery('u_joining_date', queryTime);
gr.query();
while(gr.next()) {
if (gr.user.email.toString() == ""){
gs.eventQueue('sn_hr_core.completion_milestone',gr,i,gr.user.manager.toString());
}else{
gs.eventQueue('sn_hr_core.completion_milestone',gr,i,gr.user.toString());
}
gs.info("Employee : "+ gr.user.name + " completed year "+i);
}}}
I have tested the same in my PDI, let me know if you have any further queries.
Please mark this as Correct or Helpful if it helps.
Thanks and Regards,
Abhijit
Regards,
Abhijit
ServiceNow MVP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-22-2022 11:49 PM
Thanks Abhijit
in one case if I put joining date as "29-02-2004" and current date as "2024-03-01 00:00:007" or "2024-02-29 00:00:00".
In both days it trigger notification. on 29th feb 2024 as well on 1st march 2024 as 2024 and 2004 both are leap years.
What changes will be done in code.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-23-2022 01:09 AM
Hi,
Interesting scenario. I have modified below code to consider this scenario as well, please try below,
function autoEmail() {
var i;
for (i = 5; i < 40; i= i+5) {
var gdt_today=new GlideTime();
gdt_today.addDaysUTC(-1);
var currentMonth=gdt.getMonthLocalTime();
var currentDay=gdt.getDayOfMonthLocalTime();
var gdt = new GlideDateTime();
gdt.addYearsUTC(-i);
gdt.addDaysUTC(-1);
var month = gdt.getMonthLocalTime();
var date = gdt.getDayOfMonthLocalTime();
if(month==2 && date==29){
if(!(currentMonth==2 && currentDay==29)){
queryTime = gdt.getDate();
}
}
else{
gdt.addDaysUTC(1);
queryTime = gdt.getDate();
}
var gr = new GlideRecord('sn_hr_core_profile');
gr.addQuery('user.u_business_unit', 'US');
gr.addQuery('u_joining_date', queryTime);
gr.query();
while(gr.next()) {
if (gr.user.email.toString() == ""){
gs.eventQueue('sn_hr_core.completion_milestone',gr,i,gr.user.manager.toString());
}else{
gs.eventQueue('sn_hr_core.completion_milestone',gr,i,gr.user.toString());
}
gs.info("Employee : "+ gr.user.name + " completed year "+i);
}}}
Newly added logic checks whether current year is leap or not if current year is leap year then for 1st march, it will not trigger notification, it will trigger on 29th Feb. If current year is not leap year then on 1st March it will trigger notification for 29th Feb joiners.
Please mark this as Correct or Helpful if it helps.
Thanks and Regards,
Abhijit
Regards,
Abhijit
ServiceNow MVP