Incident

ServiceNow40
Tera Contributor

Update the state of all active incidents where the priority is 1, and the "Caller" is part of the "IT" department. The update should set the state to "In Progress" but must avoid any performance issues or database locking problems in a system with a very large number of incident records (over 1 million). Additionally, you must ensure that the update does not inadvertently affect incidents that don’t meet the criteria or bypass security constraints.

 

Any idea how to build above task code can you please help me.

1 REPLY 1

Runjay Patel
Giga Sage

Hi @ServiceNow40 ,

 

You can write script like below. This is the best way to update incident as per my knowledge.

 

var BATCH_SIZE = 100; // Number of records to process per batch
    var updatedCount = 0;

var incidentGr = new GlideRecord('incident');
    incidentGr.addQuery('priority', 1); // Priority 1
    incidentGr.addQuery('state', '!=', 2); // Not already "In Progress" (optional safeguard)
    incidentGr.addQuery('active', true); // Active incidents only
    incidentGr.addJoinQuery('sys_user', 'caller_id', 'sys_id')
              .addCondition('department.name', 'IT'); // Caller in IT department
    incidentGr.orderBy('sys_created_on'); // Optional: Ensure consistent ordering
    incidentGr.query();

    while (incidentGr.next()) {
var batchCount = 0;
     do {
            
                incidentGr.state = 2; // "In Progress"
                incidentGr.update();
                batchCount++;
                updatedCount++;
            }
        } while (incidentGr.next() && batchCount < BATCH_SIZE);

gs.sleep(500);
            }

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------