Issue: ATF Schedule Condition Not Triggering with Script Include (Odd/Even Week Logic)
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
I was trying to run an ATF Schedule (sys_atf_schedule) based on odd-week Mondays (1st, 3rd, 5th) using a Script Include.
I created a Script Include to identify odd weekday occurrences:
isOddWeekday: function(targetDay) {
gs.log("inside function");
var now = new GlideDateTime();
// Check if today is the required weekday
if (now.getDayOfWeekLocalTime() != targetDay) {
return -1;
}
// First day of month
var firstOfMonth = new GlideDateTime(now);
firstOfMonth.setDayOfMonthLocalTime(1);
// Find first occurrence of that weekday
var probe = new GlideDateTime(firstOfMonth);
while (probe.getDayOfWeekLocalTime() != targetDay) {
probe.addDaysLocalTime(1);
}
var firstDay = new GlideDateTime(probe);
// 1st, 3rd, 5th occurrence
var occ1 = new GlideDateTime(firstDay);
var occ3 = new GlideDateTime(firstDay);
occ3.addWeeksLocalTime(2);
var occ5 = new GlideDateTime(firstDay);
occ5.addWeeksLocalTime(4);
var today = now.getLocalDate().getValue();
if (
today == occ1.getLocalDate().getValue() ||
today == occ3.getLocalDate().getValue() ||
(occ5.getMonthLocalTime() == now.getMonthLocalTime() &&
today == occ5.getLocalDate().getValue())
) {
gs.log("Inside pass true")
return 1;
}
return -1;
},
This works correctly when tested independently.
In ATF Schedule condition, I used:
var result = oldWeekMonday();
if (result == true) {
answer = true;
} else {
answer = false;
}
function oldWeekMonday() {
var day = current.run_dayofweek;
return new ScheduleUtils().isOddWeekday(day);
}
- Schedule does not trigger
- But if I use: answer = true; it works fine
Could anyone please advise on this how to approach ?
Thanks,
Pavithra
1 REPLY 1
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
Hi @pavi12
In ATF schedule condition answer must be true or false and you mention 1 and -1.
So, update your script to return true/false.
Updated script:
isOddWeekday: function (targetDay) {
var now = new GlideDateTime();
if (now.getDayOfWeekLocalTime() != targetDay) {
return false;
}
var firstOfMonth = new GlideDateTime(now);
firstOfMonth.setDayOfMonthLocalTime(1);
var probe = new GlideDateTime(firstOfMonth);
while (probe.getDayOfWeekLocalTime() != targetDay) {
probe.addDaysLocalTime(1);
}
var occ1 = new GlideDateTime(probe);
var occ3 = new GlideDateTime(probe);
occ3.addWeeksLocalTime(2);
var occ5 = new GlideDateTime(probe);
occ5.addWeeksLocalTime(4);
var today = now.getLocalDate().getValue();
if (
today == occ1.getLocalDate().getValue() ||
today == occ3.getLocalDate().getValue() ||
(
occ5.getMonthLocalTime() == now.getMonthLocalTime() &&
today == occ5.getLocalDate().getValue()
)
) {
return true;
}
return false;
}
Script:
var result = oldWeekMonday();
answer = (result === 1);
function oldWeekMonday() {
var day = current.run_dayofweek;
return new ScheduleUtils().isOddWeekday(day);
}
Try this and let us know.
***********************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful." This action benefits both the community and me.
***********************************************************************************************************************
Regards
Shaqeel