Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Regarding 3 strike rule

ShubhangiA77524
Tera Contributor

I have a code for 3 strike policy, is there any way around or optimization for it?

var MANILA_TZ = 'Asia/Manila';
var SCHED_ID = '06d3639edb34e510ee8fdf0bd396199e';
var dc = new DurationCalculator();
dc.setSchedule(SCHED_ID, MANILA_TZ);
 
// Cache for Catalog Item configurations (prevents repeated lookups in loop)
var itemCache = {};
 
// Target SC Tasks for this specific group & state
var tsk = new GlideRecord('sc_task');
tsk.addEncodedQuery('active=true^state=-5^u_pending_reason=Awaiting Information from User');
tsk.addQuery('assignment_group.name', 'CESO Network - MS : IP Telephony');
tsk.query();
 
while (tsk.next()) {
    var now = new GlideDateTime();
 
    var objRITM = new GlideRecord('sc_req_item');
    if (!objRITM.get(tsk.request_item)) {
        gs.warn('--- Skipping task ' + tsk.number + ' (RITM not found)');
        continue;
    }
    var recipient = objRITM.request.requested_for;
    if (!recipient) {
        gs.warn('--- No recipient found for ' + tsk.number);
        continue;
    }
 
    var config = {
        first_evt: 'poc.3strike.sc.event.1',
        second_evt: 'poc.3strike.sc.event.2',
        third_evt: 'poc.3strike.sc.event.3',
        fourth_evt: 'poc.3strike.sc.event.4',
        req_endstate: 3
    };
 
    var catItemId = objRITM.cat_item.toString();
    if (itemCache[catItemId]) {
        config = itemCache[catItemId];
    } else {
        var cat_item_gr = new GlideRecord('sc_cat_item');
        if (cat_item_gr.get(catItemId)) {
 
            if (!cat_item_gr.u_3strike_rule_followup_event.nil()) {
                try {
                    var ev = JSON.parse(cat_item_gr.u_3strike_rule_followup_event.toString());
                    config.first_evt = ev[1] || config.first_evt;
                    config.second_evt = ev[2] || config.second_evt;
                    config.third_evt = ev[3] || config.third_evt;
                    config.fourth_evt = ev[4] || config.fourth_evt;
                } catch (e) {
                    gs.warn('Invalid JSON in u_3strike_rule_followup_event for ' + cat_item_gr.name);
                }
            }
            if (!cat_item_gr.u_state_after_3strike_rule.nil()) {
                config.req_endstate = cat_item_gr.u_state_after_3strike_rule.getValue() || config.req_endstate;
            }
            itemCache[catItemId] = config;
        }
    }
 
    // Determine if follow-up due
    // Use GlideDateTime comparison directly
    var lastFollowUp = tsk.u_date_last_follow_up.getGlideDateTimeValue();
    if (lastFollowUp && lastFollowUp.after(now)) {
        continue;
    }
 
    // Determine strike count and event
    var ctr = parseInt(tsk.u_number_of_follow_up || 0);
    var evt = '';
    switch (ctr) {
        case 0:
            evt = config.first_evt;
            break;
        case 1:
            evt = config.second_evt;
            break;
        case 2:
            evt = config.third_evt;
            break;
        case 3:
            evt = config.fourth_evt;
            break;
    }
 
    if (!evt) {
        continue;
    }
 
    // Trigger follow-up event
    gs.eventQueue(evt, objRITM, recipient, '');
 
    // Compute next follow-up using Duration Calculator (9 business hours later)
    // 9 hours = 32400 seconds
    var nextFollowUpGDT = dc.calcDuration(now, now, 32400);
 
    if (ctr < 2) {
        // Strike 1 or 2
        ++ctr;
        tsk.u_number_of_follow_up = ctr;
        tsk.u_date_last_follow_up = nextFollowUpGDT;
    } else {
        // Strike 3 → Close task
        tsk.u_number_of_follow_up = 3;
        tsk.state = config.req_endstate || 4;
        tsk.work_notes = 'No Response from user (3 strike rule)';
        gs.info('--- Closing SC Task ' + tsk.number + ' (Final Strike). State set to ' + tsk.state);
    }
    tsk.update();
}
5 REPLIES 5

Ok.  I'm submitting a ticket, you ask a question.  Its pending.  I get a strike, and then I answer.  I'm out of the three strikes period.  Now you ask me another question.  It's pending again.
Is this a NEW "three strikes interval"?