Condition Script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-02-2023 09:31 AM
Hi team, is there a possibility to add a custom field to the scheduled jobs form, allowing the user to choose how many months until the scheduled job is triggered again? For example, if they want it to run on a quarterly basis, they can simply select '3 months' instead of using a Condition Script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-03-2023 06:31 AM
Hi @Rajesh_Bhise how can i implement the code
var monthsUntilNextRun = current.months_until_next_run;
var currentDate = new GlideDate();
currentDate.addMonths(monthsUntilNextRun);
current.next_action = currentDate;
and override the OOTB includes that trigger the execution of the scheduled job ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-03-2023 06:50 AM - edited 09-03-2023 06:52 AM
Hi @yoli1
Please don't alter a system table, or override system behavior, as that can lead to problems. I doubt you can affect the System Scheduler that way. Instead create a new Business Calendar, and add Entries for the desired date/time you want the Scheduled job to run, see my test below:
The job I used to test has:
And I checked and found:
(the earlier three are from me testing the job, the later two from when the job ran.) I see I need to consider Time Zone, but that can be done. So use a new Business calendar with entries defined as you like.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-03-2023 07:07 AM
Hi @Bert_c1 Thanks. so if i'am understanding right rather than creating a new field with a BS script on scheduled job table and override the OOTB (which can lead to problems) i should use Business Calendar with Run set to Business Calendar : Entry Start. if i want a quarter period i have it OOB but if i need other requirement i should create a new Business Calendar am i right?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-03-2023 07:55 AM
Hi Yoli1,
Yes, just like the "Quarter" and "Fiscal Quarter" business calendars are set. You avoid any modification to system behavior and achieve your goal of the job running on days that you desire.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-03-2023 12:36 PM - edited 09-03-2023 12:40 PM
Hi @yoli1
If you do decide to add a field to the sysauto_script table, that specified the number of months for the next execution. And the job is defined to 'Run' "Once", then you can use the following script logic at the end of the script to re-schedule the job:
// This logic can be added to a scheduled job defined as 'Run' "Once"
var schJob = new GlideRecord('sysauto_script');
// Find the record of the this scheduled job to update next execution
schJob.addQuery('sys_id', '0fa4fded47c17110753923dbd36d43b5');
schJob.query();
if (schJob.next()) {
var now = new GlideDateTime();
// Set next run to the value of the u_number_of_months_to_run_again integer field on the job record
now.addMonths(schJob.u_number_of_months_to_run_again);
schJob.run_start = now;
schJob.update();
}
(Note: the above requires knowing the sys_id value of the job. Or you may query on the name field.)
Seems it may be better that creating a Business Calendar for each user who will be creating Scheduled Jobs. Adding a custom field (u_number_of_months_to_run_again) to a table is fine, I myself hesitate to do that on a system table if I can find an alternative solution.