Create Incident if catalog task is not closed in 14 days

Aaviii
Tera Contributor

Hi,

I have requirement to create incident record if Catalog task is not closed in 14 days from the day its created. Once Incident created it should also send the notification email to recipient.

 

Any help would be highly appreciated.

 

Regards

Utkarsh

3 REPLIES 3

Sohail Khilji
Kilo Patron
Kilo Patron

Hi @Aaviii ,

 

Create a scheduled job which runs every day to check for task which are not closed for last 14 days and create incident using gliderecord and trigger event to send notificaiton.

 

You can also use flowdesginer to easily attain this... let me know if you need futher helpp...


☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect

Its_Azar
Tera Guru

Hey there @Aaviii 

you can use a combo of Business Rules and Scheduled Jobs.

Write a business rule on the Catalog Task table (`sc_task`) that triggers when a record is inserted. In the business rule script, check if the task has been open for 14 days. If it has, create an incident record and send a notification email.

(function executeRule(current, previous /*, g*/) {
   
    var openDuration = gs.dateDiff(current.sys_created_on, gs.now(), true);
    if (openDuration >= 14) {

        var incGr = new GlideRecord('incident');
        incGr.initialize();
        incGr.short_description = 'Catalog Task Not Closed';
        incGr.insert();
        

    }
})(current, previous);

 

 

Now, create a scheduled job that runs daily or periodically to check for Catalog Tasks that have been open for 14 days and haven't been closed yet.

 

// Scheduled job script
(function () {
    var taskGr = new GlideRecord('sc_task');
    taskGr.addQuery('active', true);
    taskGr.addQuery('state', '!=', 'closed');
    taskGr.addQuery('sys_created_on', '<=', gs.daysAgoStart(14));
    taskGr.query();
    
    while (taskGr.next()) {
       
        var incGr = new GlideRecord('incident');
        incGr.initialize();
        incGr.short_description = 'Catalog Task Not Closed';
        incGr.insert();
  
    }
})();

 

 

 Implement the logic to send notification emails in both the business rule and scheduled job scripts. You can use the `GlideEmailOutbound` API to send emails.

 

If this helps kindly accept the response thanks much.

☑️ If this helped, please mark it as Helpful or Accept Solution so others can find the answer too.




Kind Regards,

Mohamed Azarudeen Z

Developer @ KPMG

 Microsoft MVP (AI Services), India

Maddysunil
Kilo Sage

@Aaviii 

You can create a BR:

 

(function executeRule(current) {
    var openTime = current.getValue('opened_at');
    var currentTime = new GlideDateTime();
    var daysOpen = gs.dateDiff(openTime.getDisplayValue(), currentTime.getDisplayValue(), true);
    
    if (daysOpen >= 14 && current.getValue('state') != 7 /* State 7 represents 'Closed' */) {
        // Create an Incident record
        var incident = new GlideRecord('incident');
        incident.initialize(); // Initialize the Incident record
        incident.short_description = 'Catalog Task not closed in 14 days'; // Set Incident short description
        incident.description = 'Catalog Task ' + current.number + ' has been open for 14 days and is not closed yet.'; // Set Incident description
        incident.insert(); // Insert the Incident record
        
        // Send notification email
        gs.eventQueue('incident.created', incident, '', ''); // Trigger event for sending notification email
    }
})(current);

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks