Script to close all In_progress incidents

Parth_S
Tera Contributor

I want to fetch the all the "In_Progress" incidents and finally change the state to "Closed", how can i achieve this ?

Using Background script (Other way would also appreciated).

2 ACCEPTED SOLUTIONS

Anand Kumar P
Giga Patron
Giga Patron

Hi @Parth_S ,

 

You can do it via fix script or background script, but do it in lower instance.

 

As per businesses usecases will not close the bulk inprogress incidents to closed.

 

var updatedCount = 0; 

var grIncident = new GlideRecord('incident');

grIncident.addQuery('state', 2);

grIncident.setLimit(10)

grIncident.query();

while (grIncident.next()) {

    // Disable business rules and workflows

    grIncident.setWorkflow(false); 

    grIncident.state = 7

    grIncident.update();

    updatedCount++;

}

 

gs.print(updatedCount + " incidents updated to Closed.");

 

If my response helped, please mark it as the accepted solution and give a thumbs up👍.
Thanks,
Anand

View solution in original post

Valmik Patil1
Kilo Sage

Hello @Parth_S ,

You can run below background script.

 

// Create a GlideRecord object for the 'incident' table
var gr = new GlideRecord('incident');

// Query for incidents where the state is "In Progress" (assuming "In Progress" state is a value in the state field)
gr.addQuery('state', 2);
gr.query();
while (gr.next()) {
    gr.state = '7'; // Set the state to "Closed"
    gr.update(); // Update the record in the database
}

gs.info('All "In Progress" incidents have been closed.');

 

Thanks,

Valmik Patil

View solution in original post

4 REPLIES 4

Anand Kumar P
Giga Patron
Giga Patron

Hi @Parth_S ,

 

You can do it via fix script or background script, but do it in lower instance.

 

As per businesses usecases will not close the bulk inprogress incidents to closed.

 

var updatedCount = 0; 

var grIncident = new GlideRecord('incident');

grIncident.addQuery('state', 2);

grIncident.setLimit(10)

grIncident.query();

while (grIncident.next()) {

    // Disable business rules and workflows

    grIncident.setWorkflow(false); 

    grIncident.state = 7

    grIncident.update();

    updatedCount++;

}

 

gs.print(updatedCount + " incidents updated to Closed.");

 

If my response helped, please mark it as the accepted solution and give a thumbs up👍.
Thanks,
Anand

Mark Manders
Mega Patron

What is your business case to do this? 

What is the logic you need to run on this (or not run on this)? 

Closing incidents will trigger notifications and doing it by script will not set close notes/close codes or provide the user with any update on the incident they have created.

 

Be really careful with these kinds of requirements. But if it's legit:

 

var grInc = new GlideRecord('incident');

grInc.addEncodedQuery('add_your_incoded_query_here'; // could be as simple as 'state=2' 

grInc.query();

while(grInc.next()){

grInc.setValue('state','7'); // closed

grInc.work_notes = 'Closed via script because .......';

grInc.add other fields you need updated like close notes/code/etc

//grInc.setWorkflow(false); // if you don't want to trigger anything

grInc.update();

}


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Valmik Patil1
Kilo Sage

Hello @Parth_S ,

You can run below background script.

 

// Create a GlideRecord object for the 'incident' table
var gr = new GlideRecord('incident');

// Query for incidents where the state is "In Progress" (assuming "In Progress" state is a value in the state field)
gr.addQuery('state', 2);
gr.query();
while (gr.next()) {
    gr.state = '7'; // Set the state to "Closed"
    gr.update(); // Update the record in the database
}

gs.info('All "In Progress" incidents have been closed.');

 

Thanks,

Valmik Patil

Unique45
Mega Sage

Hello @Parth_S ,

You can run below code in the background script or you can create fix script and below code and run fix script.

 

var incident = new GlideRecord('incident');
incident.addQuery('state', '2');
incident.query();

while(incident.next()){
    incident.state = '7';
    incident.close_code = 'Duplicate'; // add resolution code
    incident.close_notes = 'incident has been closed'; // add  resolution note
    incident.update();
}

 

 

Please mark correct/helpful if this helps you!