
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-26-2017 02:24 PM
I am new to scripting and need assistances with creating a conditional script for a scheduled job.
I need the job to execute based on the 1st day of the month, 2nd day of the month, every Friday, and last day of the month.
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-26-2017 03:23 PM
Hey Kristy,
That should be pretty doable as well.
Try adding this script to the script block (remove the "gs.print" lines) of a daily run job and then doing whatever you need in the if condition. It will only do what you need on 1st 2nd last and Fridays.
Run the below code as is in the background scripts to see the values printed out, since today is Friday you will see "hello" tomorrow you would not see "hello" since it is in the if block
var gdt2 = new GlideDateTime();
gdt2.setDayOfMonthLocalTime(31);
var gdt = new GlideDateTime();
var checkLast = new GlideDateTime();
checkLast = checkLast.setDayOfMonthLocalTime(31);
if(gdt.getDayOfWeekLocalTime() == 5 || gdt.getDayOfMonthLocalTime() == 1|| gdt.getDayOfMonthLocalTime()() == 2 || gdt2.getDayOfMonthLocalTime() == gdt.getDayOfMonth()){
//run script here
gs.print('hello');
}
gs.print(gdt.getDayOfWeekLocalTime());
gs.print(gdt.getDayOfMonthLocalTime());
gs.print(gdt2.getDayOfMonthLocalTime());
Hopefully that helps!
Best,
Andrew
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-26-2017 02:41 PM
Hey Kristy,
What are you trying to do? ServiceNow makes scheduled jobs pretty easy to do, and often does not require any scripting. Go into System Definition > Scheduled Jobs and from there you will be prompted some options. After you select one you will be greeted by the scheduler, in which you can choose Run "Weekly" on Friday at 8:00
You can then set up another run monthly on the 1st, 2nd, 31st day of the month
That should cover the times you need said action to occur. If you need the Friday and days of month not to duplicate, you will need to script a condition to not run the script if its the first second or last day of month.
Do you need that?
Hopefully that helps!
Best,
Andrew

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-26-2017 02:56 PM
I was attempting to create one job that would kick off 4 times in one month without creating four schedules.
I guess I am attempting to make the schedule to difficult. Yes, I would not need for a duplicate if a Friday fell on the last day of the month.
Kristy Stevens
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-26-2017 03:23 PM
Hey Kristy,
That should be pretty doable as well.
Try adding this script to the script block (remove the "gs.print" lines) of a daily run job and then doing whatever you need in the if condition. It will only do what you need on 1st 2nd last and Fridays.
Run the below code as is in the background scripts to see the values printed out, since today is Friday you will see "hello" tomorrow you would not see "hello" since it is in the if block
var gdt2 = new GlideDateTime();
gdt2.setDayOfMonthLocalTime(31);
var gdt = new GlideDateTime();
var checkLast = new GlideDateTime();
checkLast = checkLast.setDayOfMonthLocalTime(31);
if(gdt.getDayOfWeekLocalTime() == 5 || gdt.getDayOfMonthLocalTime() == 1|| gdt.getDayOfMonthLocalTime()() == 2 || gdt2.getDayOfMonthLocalTime() == gdt.getDayOfMonth()){
//run script here
gs.print('hello');
}
gs.print(gdt.getDayOfWeekLocalTime());
gs.print(gdt.getDayOfMonthLocalTime());
gs.print(gdt2.getDayOfMonthLocalTime());
Hopefully that helps!
Best,
Andrew

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-30-2017 07:22 AM
Andrew,
Thank you for the conditional script. I have been able to confirm the script works however the scheduled job is not firing.
Do I need to add an answer to confirm the condition was met to have the job fire?
I have created a template that will be scheduled based on this condition. If I do Execute Now, the task is not created for my template.
Kristy Stevens