scheduled job

Kongaleti Navee
Tera Contributor

can you write a scheduled job script to check if there are 5 incident created in a day with the 'hardware' category, then you need to create a problem ticket and assign it to the manager of the hardware group.

4 REPLIES 4

Sohithanjan G
Kilo Sage
Kilo Sage

Hi @Kongaleti Navee 

 

You can create a schedule job to run in the mid night daily at 11:50 with below script

function checkIncidentsAndCreateProblem() {
    // Set the date range for today
    var now = new GlideDateTime();
    var startOfDay = new GlideDateTime();
    startOfDay.setDisplayValue(now.getDate().getLocalDate().toString() + " 00:00:00");

    // Query for incidents created today with category 'hardware'
    var incidentGr = new GlideRecord('incident');
    incidentGr.addQuery('category', 'hardware');
    incidentGr.addQuery('sys_created_on', '>=', startOfDay);
    incidentGr.query();

    // Check if there are at least 5 incidents
    if (incidentGr.getRowCount() >= 5) {
        // Create a new problem ticket
        var problemGr = new GlideRecord('problem');
        problemGr.initialize();
        problemGr.short_description = 'Hardware incidents exceed threshold';
        problemGr.assignment_group = hardwareGroupMgr; //sys_id of hardware group
        problemGr.insert();
    }
}

checkIncidentsAndCreateProblem();

 

 

Please mark as Accepted Solution if this solves your query and HIT Helpful if you find my answer helped you. This will help other community mates too..:)

Murthy Ch
Giga Sage

Hi @Kongaleti Navee 

Did you tried anything? If so, please share what you tried and where you stuck.

Happy to help:)

 

Thanks,
Murthy

Vishwa Pandya19
Mega Sage

Hi Kongaleti,

 

The below code should work for you.

var inc = new GlideRecord('incident');
inc.addEncodedQuery('sys_created_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()^category=hardware');
inc.query();

var count = inc.getRowCount();

if(count >= 5){
	var prb = new GlideRecord('problem');
	prb.initialize();
	prb.short_description = 'Hardware category more than 5 incidents';
	prb.category = 'hardware';
	prb.insert();
}

 

If my answer helped you in anyway please mark it as correct.

Harish KM
Kilo Patron
Kilo Patron

Hi @Kongaleti Navee why dont you use flow designer with trigger as scheduled and have a look up on incident record , no code required

Regards
Harish