create a incidents automatically on a specific date

DavidPrice
Tera Contributor

I would like to create a schedule that create a incident on a specific date.

 

from example 

 

08/07/2025

15/07/2025

08/08/2025

etc

 

I have created a template to create the incident.

I can get it working on the week/day etc. 

 

but can not see a why that I can type my own date's in for it to run 

11 REPLIES 11

@DavidPrice In this case you need to create a scripted schedule job with the following script.

 

(function executeScheduledJob() {
    // Define the specific dates (format: yyyy-MM-dd)
    var specificDates = [
        "2025-07-01",
        "2025-07-03",
        "2025-07-04",
        "2025-07-07",
        "2025-07-21"
    ];

    // Get today's date in yyyy-MM-dd format (UTC)
    var today = new GlideDate();
    var todayStr = today.getByFormat("yyyy-MM-dd");

    // Check if today matches one of the specific dates
    if (specificDates.indexOf(todayStr) >= 0) {
        gs.info("Script executed on scheduled specific date: " + todayStr);

        // --- YOUR BUSINESS LOGIC GOES HERE ---
        // e.g., create record, trigger flow, clean data, etc.
        
    } else {
        gs.info(" Skipped execution: " + todayStr + " is not in the allowed date list.");
    }

})();

Hi Tera
Thank for your help, I have had a goo at it but it is not working I think I must be missing a part 

DavidPrice_0-1752243063327.png

 

 
// Define the list of allowed execution dates
var specificDates = [
    "2025-07-01",
    "2025-07-03",
    "2025-07-04",
    "2025-07-07",
    "2025-07-11"
];

// Get today's date in yyyy-MM-dd format
var today = new GlideDate();
var todayStr = today.getByFormat("yyyy-MM-dd");

// Check if today is in the list
if (specificDates.indexOf(todayStr) >= 0) {
    // --- BUSINESS LOGIC: Create an incident using the "stagenow" template ---

    var gr = new GlideRecord('incident');
    gr.initialize();

    // Apply the 'stagenow' template (from sys_template)
    var template = new GlideTemplate();
    template.setTemplate('incident', 'stagenow'); // table name, template name
    template.apply(gr);

    // Insert the record
    var newSysId = gr.insert();

    // Log the result
    gs.info(' Incident created using template "stagenow". Number: ' + gr.number);
} else {
    // Log skipped execution
    gs.info(" Skipped execution: ' + todayStr + ' is not in the allowed date list.");
}

Masthan Sharif
Tera Guru

Hi @DavidPrice ,

Hope you're doing well!

 

If you would like the scheduled job to run at specific dates like 08/07/2025 and 15/07/2025, you can use something like below in your scheduled job condition

 

var answer = false;
// Only run on your specified dates
var today = new GlideDateTime().getDate(); // e.g., "2025-07-07"
var datesToRun = [
  "2025-07-08",
  "2025-07-15",
  "2025-08-08"
  // add more dates here
];

if (datesToRun.indexOf(today) > -1) {
  // Run only on specified dates
  answer = true;
}

  

 

Best Regards,

Sharif

I have a good at this script

DavidPrice_1-1752243200210.png

 

but I am not getting any results

 

AndersBGS
Tera Patron
Tera Patron

Hi @DavidPrice ,

 

 why? why would you create an incident on specific date? what is the business case? An incident is something in PROD that worked yesterday but not today according to ITIL and best practice.

 

If my answer has helped with your question, please mark my answer as the accepted solution and give a thumbs up.

Best regards
Anders

Rising star 2024
MVP 2025
linkedIn: https://www.linkedin.com/in/andersskovbjerg/