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();
}
4 REPLIES 4

Uncle Rob
Kilo Patron

There's many many ways to do a 3 strike rule.
Code as presented, I have no idea if this is a BR, a script include, or just a one time run background script.
Why not start with telling us what you've got and what you WANT to happen?
3 strikes of WHAT?


I have to make a schedule job that should run on 7th business day and my requirement is:
assume like i have created a ticket from a catalog item and ritm and sc task are generated so  3 strike rule means , when ticket is created and state is pending and reason is awaiting info from user , if user does not reply to that ticket then a 1 follow up should send to him in email body, then after 9 hrs 2 follow up, then 3 follow , after 3 follow up if requestor does not reply then 4th follow up is sent and the state of sc task is made closed incomplete with work note- No response from user.

And since states can move back and forth, and since customers can be responsive and then not responsive, when does the "wait for three strikes" period begin, end, and reset?

wait for three strikes period begins when state is pending and there is no response from user within the schedule that is 7am-4pm