Required a query to fetch a data

Bhagyashri Sor1
Tera Contributor

Hello

 

I have an requirement that I am relating with the Incident. The Requirement is as follows.

 

Let's consider the caller raised an incident in Sep, oct, Nov, Dec and Jan and he closed the incident of oct and Jan. Then I want to close the incident for Nov and Dec not for Sep for the same caller.

 

Please help me out with the solution for this.

3 REPLIES 3

Gustav Aldenbra
Kilo Sage

Hi @Bhagyashri Sor1

You should be able to do this with a business rule like the one below. I recommend that you test this well before taking it into production as it can affect performance if there are many records.

I have not tested this myself but it should work.

 

(function executeRule(current, previous /*null when async*/) {
    // Check if the current incident is closed; if not, exit
    if (current.state != 7) { // Assuming '7' is the code for 'Closed'
        return;
    }

    // Get the caller's ID from the current incident
    var callerId = current.caller_id;

    // Find the earliest and latest closed incident dates for this caller
    var grIncidents = new GlideRecord('incident');
    grIncidents.addQuery('caller_id', callerId);
    grIncidents.addQuery('state', 7); // Closed incidents
    grIncidents.orderBy('closed_at');
    grIncidents.query();
    
    if (!grIncidents.next()) {
        // No other closed incidents found for this caller
        return;
    }
    
    // Store the earliest closed date
    var earliestClosedDate = grIncidents.closed_at;
    
    // Find the latest closed incident date
    grIncidents.orderByDesc('closed_at');
    grIncidents.query();
    grIncidents.next();
    var latestClosedDate = grIncidents.closed_at;
    
    // Now, close incidents for the caller that are between these dates, excluding the boundary incidents
    var grUpdateIncidents = new GlideRecord('incident');
    grUpdateIncidents.addQuery('caller_id', callerId);
    grUpdateIncidents.addQuery('opened_at', '>', earliestClosedDate);
    grUpdateIncidents.addQuery('opened_at', '<', latestClosedDate);
    grUpdateIncidents.addQuery('state', '!=', 7); // Not already closed
    grUpdateIncidents.query();
    
    while (grUpdateIncidents.next()) {
        // Set to Closed state; customize as per your instance's field and value
        grUpdateIncidents.state = 7; 
        grUpdateIncidents.work_notes = 'Automatically closed based on related incidents closure criteria.';
        grUpdateIncidents.update();
    }
})(current, previous);

 

AndersBGS
Tera Patron
Tera Patron

Hi @Bhagyashri Sor1 ,

 

Why? November and December incident can be for something not related to the other incidents? 

 

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

 

best regards

Anders 

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/

Sohithanjan G
Kilo Sage
Kilo Sage

Hi @Bhagyashri Sor1 , 

To achieve this requirement, you can create async Business Rule that triggers when an incident is closed. The Business Rule would then check if there are any open incidents for the same caller in the months after the closed incident. If there are open incidents, they can be closed automatically.

(function executeRule(current, previous /*null when async*/) {
    var caller = current.caller_id;
    var closedAt = new GlideDateTime(current.closed_at);
    var closedMonth = closedAt.getMonth();
    
    var openIncidents = new GlideRecord('incident');
    openIncidents.addQuery('caller_id', caller);
    openIncidents.addQuery('active', true);
    openIncidents.addQuery('sys_created_on', '>', gs.beginningOfMonth(closedMonth));
    openIncidents.addQuery('sys_created_on', '<', gs.endOfMonth(closedMonth));
    openIncidents.query();

    while (openIncidents.next()) {
        openIncidents.state = 6;
        openIncidents.update();
    }
})(current, previous);

 

Mark as accepted solution & hit helpful button !!!

 

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..:)