scheduled job periodically

manumanoj
Tera Contributor

Hi

In my incident table, I created a field called maintenance, so in that field, once and periodically, like a scheduled job, when I am trying to execute it periodically, nothing happens.

This is the code

var gr = new GlideRecord('incident');
gr.addEncodedQuery('schedule_maintenance=periodically^repeated_intervelISNOTEMPTY^starting_dateISNOTEMPTY()');
gr.query();

while (gr.next()) {
    if (gr.u_schedule_maintenance == 'periodically' && gr.u_repeated_intervel && gr.u_starting_date) {
        var startDateTime = new GlideDateTime(gr._starting_date);
        var nextScheduledTime = calculateNextScheduledTime(startDateTime, gr.u_repeated_intervel);
        var currentTime = new GlideDateTime();
        gs.info('Current Time: ' + currentTime);
        gs.info('Next Scheduled Time: ' + nextScheduledTime);
        if (currentTime.equals(nextScheduledTime) || currentTime.after(nextScheduledTime)) {
            if (createIncident(gr)) {
                gs.info('Incident created successfully for record ' + gr.getUniqueValue());
                gr.schedule_maintenance = 'None'; // Assuming you want to set it to 'None' after creating the incident
                gr.update();
                gs.info('Record updated successfully.');
            } else {
                gs.error('Failed to create incident for record ' + gr.getUniqueValue());
            }
        }
    }
}

function calculateNextScheduledTime(startDateTime, repeatedIntervalInDays) {
    var nextScheduledTime = new GlideDateTime(startDateTime);
    nextScheduledTime.addDays(repeatedIntervalInDays);
    return nextScheduledTime;
}





3 REPLIES 3

Siddhesh Gawade
Mega Sage
Mega Sage

Hello @manumanoj , 

As a first noticable difference is the Name of schedule Manitance field. In your script you are using two different name is this are the same field name or different ? 

If you are trying to use the same Field then corrent the code with exact backend name of code. ? 

If this is not the case then can you please explain the requirement?

Hi Siddhesh

Names are the same. My requirement is in my incident table. I have created a field called schedules maintenance and added two values: 'once' and 'periodically' if I select periodically and need to set repeated intervals and started time. This is in my incident table.
Create a new scheduled job. There, I will set the interval to every 5 minutes, so it will check in the incident table that if scheduled maintenance is periodically and repeated interval is not empty and the start date is not empty. if condition true create incident every 5 minutes

 

Bert_c1
Kilo Patron

This line has an error with the field name:

 

var startDateTime = new GlideDateTime(gr._starting_date);

 

missing the initial "u".