Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

how to restrict incident creation from calendar invites for DL

RudhraKAM
Tera Guru

As of now we are creating an Incidents via Inbound action , but when there is a calendar invite , still it is creating Incident , How to restrict that ?

 

I am not able to restrict subject , as it can be any 

 

RudhraKAM_0-1764942196011.png

 

47 REPLIES 47

pratyusha11
Tera Contributor
if (elidProperty) {
    // Process comma-separated values exactly like the recipient script
    var ids = elidProperty.split(',').map(function(id) {
        return id.trim();
    }).join(',');

    var userGR = new GlideRecord('sys_user');
    var qc = userGR.addQuery('u_elid', 'IN', ids);
    qc.addOrCondition('user_name', 'IN', ids);
    
    userGR.query();
    if (userGR.next()) {
        username = userGR.name.toString();
    }
}

pratyusha11
Tera Contributor
var encodedQueries = [
    'active=true^task.sys_class_name=change_request^task.stateIN3,4^sla=40ccfbee2b8b5e1035d6fa6c5e91bf72',
    'active=true^task.sys_class_name=change_task^task.stateIN3,4^sla=f89f73e22bcb5e1035d6fa6c5e91bfac',
    'active=true^task.sys_class_name=change_task^task.stateIN3,4^sla=3a7dd000eb048310d15afcbbcad0cd3b'
];

function cancelFlowContextsForTaskSLA(taskSlaSysId, reason) {
    var ctx = new GlideRecord('sys_flow_context');
    ctx.addQuery('source_table', 'task_sla');
    ctx.addQuery('source_record', taskSlaSysId);
    ctx.addQuery('state', 'WAITING');
    ctx.query();

    while (ctx.next()) {
        sn_fd.FlowAPI.cancel(ctx.getUniqueValue(), reason);
    }
}

// AC5: records the cancellation for reporting - one metric_instance row per
// cancelled Task SLA (for count reporting) plus a sys log entry with details
function recordSLACancellationMetric(taskSLA, taskGR, closedDateTime) {
    var metric = new GlideRecord('u_task_sla_cancellation_metric');
    metric.initialize();
    metric.u_task_sla = taskSLA.getUniqueValue();
    metric.u_task = taskGR.getUniqueValue();
    metric.u_task_number = taskGR.getValue('number');
    metric.u_sla_definition = taskSLA.getValue('sla');
    metric.u_task_table = taskGR.getRecordClassName();
    metric.u_task_state = taskGR.getValue('state');
    metric.u_sla_end_time = closedDateTime;
    metric.u_cancelled_at = new GlideDateTime();
    metric.insert();

    gs.warn(
        'Task SLA cancelled by scheduled job [Auto-close SLAs on closed changes]: ' +
        'task_sla=' + taskSLA.getUniqueValue() +
        ', task=' + taskGR.getValue('number') +
        ' (' + taskGR.getRecordClassName() + ')' +
        ', sla=' + taskSLA.getValue('sla') +
        ', task_state=' + taskGR.getValue('state') +
        ', sla_end_time_set_to=' + closedDateTime.getDisplayValue(),
        'TaskSLAAutoCancel'
    );
}

var cancelledSLACount = 0;

for (var i = 0; i < encodedQueries.length; i++) {

    var taskSLA = new GlideRecord('task_sla');
    taskSLA.addEncodedQuery(encodedQueries[i]);
    taskSLA.query();

    while (taskSLA.next()) {

        var taskGR = taskSLA.task.getRefRecord();
        var closedTimeStr = taskGR.getValue('closed_at');

        // Fallback to sys_updated_on if task is canceled and closed_at is missing
        if (!closedTimeStr) {
            if (taskGR.state == '4') { // canceled
                closedTimeStr = taskGR.getValue('sys_updated_on');
            } else {
                continue;
            }
        }

        if (!taskSLA.end_time.nil()) {
            continue;
        }

        if (new SLAAsyncQueue().isTaskQueued(taskSLA.getValue("task"))) {
            continue;
        }

        // Cancel any flow contexts tied to THIS task_sla record
        cancelFlowContextsForTaskSLA(
            taskSLA.getUniqueValue(),
            'Cancelled flow context for task_sla before forced SLA closure'
        );

        var closedDateTime = new GlideDateTime(closedTimeStr);

        taskSLA.setWorkflow(false);
        taskSLA.end_time = closedDateTime;
        taskSLA.active = false;
        taskSLA.stage = 'completed';

        var updatedSysId = taskSLA.update();

        var confirmSLA = new GlideRecord('task_sla');
        if (confirmSLA.get(updatedSysId)) {

            var slaCalc = SLACalculatorMG.newSLACalculator();
            slaCalc.loadTaskSLA(confirmSLA);
            slaCalc.calcTaskSLAs();
            slaCalc.updateTaskSLAs(confirmSLA);

            // AC5: record the cancellation for reporting + log the details
            recordSLACancellationMetric(confirmSLA, taskGR, closedDateTime);
            cancelledSLACount++;
        }
    }
}

gs.warn('Task SLA auto-cancel scheduled job complete. Total Task SLAs closed: ' + cancelledSLACount, 'TaskSLAAutoCancel');

var METRIC_DEFINITION_SYS_ID = 'PASTE_YOUR_METRIC_DEFINITION_SYS_ID_HERE';

var encodedQueries = [
    'active=true^task.sys_class_name=change_request^task.stateIN3,4^sla=40ccfbee2b8b5e1035d6fa6c5e91bf72',
    'active=true^task.sys_class_name=change_task^task.stateIN3,4^sla=f89f73e22bcb5e1035d6fa6c5e91bfac',
    'active=true^task.sys_class_name=change_task^task.stateIN3,4^sla=3a7dd000eb048310d15afcbbcad0cd3b'
];

function cancelFlowContextsForTaskSLA(taskSlaSysId, reason) {
    var ctx = new GlideRecord('sys_flow_context');
    ctx.addQuery('source_table', 'task_sla');
    ctx.addQuery('source_record', taskSlaSysId);
    ctx.addQuery('state', 'WAITING');
    ctx.query();

    while (ctx.next()) {
        sn_fd.FlowAPI.cancel(ctx.getUniqueValue(), reason);
    }
}

// AC5: records the cancellation for reporting via native Metrics
// (metric_instance tied to a metric_definition scoped to task_sla/active)
// plus a sys log entry with details of what was canceled
function recordSLACancellationMetric(taskSLA, taskGR, closedDateTime) {
    var metric = new GlideRecord('metric_instance');
    metric.initialize();
    metric.definition = METRIC_DEFINITION_SYS_ID;
    metric.id = taskSLA.getUniqueValue();      // the task_sla record this metric is about
    metric.table = 'task_sla';
    metric.field = 'active';
    metric.value = 'false';
    metric.start = taskSLA.getValue('start_time') || taskSLA.getValue('sys_created_on');
    metric.end = closedDateTime;
    metric.calculation_complete = true;
    metric.insert();

    gs.warn(
        'Task SLA cancelled by scheduled job [Auto-close SLAs on closed changes]: ' +
        'task_sla=' + taskSLA.getUniqueValue() +
        ', task=' + taskGR.getValue('number') +
        ' (' + taskGR.getRecordClassName() + ')' +
        ', sla=' + taskSLA.getValue('sla') +
        ', task_state=' + taskGR.getValue('state') +
        ', sla_end_time_set_to=' + closedDateTime.getDisplayValue(),
        'TaskSLAAutoCancel'
    );
}

var cancelledSLACount = 0;

for (var i = 0; i < encodedQueries.length; i++) {

    var taskSLA = new GlideRecord('task_sla');
    taskSLA.addEncodedQuery(encodedQueries[i]);
    taskSLA.query();

    while (taskSLA.next()) {

        var taskGR = taskSLA.task.getRefRecord();
        var closedTimeStr = taskGR.getValue('closed_at');

        // Fallback to sys_updated_on if task is canceled and closed_at is missing
        if (!closedTimeStr) {
            if (taskGR.state == '4') { // canceled
                closedTimeStr = taskGR.getValue('sys_updated_on');
            } else {
                continue;
            }
        }

        if (!taskSLA.end_time.nil()) {
            continue;
        }

        if (new SLAAsyncQueue().isTaskQueued(taskSLA.getValue("task"))) {
            continue;
        }

        // Cancel any flow contexts tied to THIS task_sla record
        cancelFlowContextsForTaskSLA(
            taskSLA.getUniqueValue(),
            'Cancelled flow context for task_sla before forced SLA closure'
        );

        var closedDateTime = new GlideDateTime(closedTimeStr);

        taskSLA.setWorkflow(false);
        taskSLA.end_time = closedDateTime;
        taskSLA.active = false;
        taskSLA.stage = 'completed';

        var updatedSysId = taskSLA.update();

        var confirmSLA = new GlideRecord('task_sla');
        if (confirmSLA.get(updatedSysId)) {

            var slaCalc = SLACalculatorMG.newSLACalculator();
            slaCalc.loadTaskSLA(confirmSLA);
            slaCalc.calcTaskSLAs();
            slaCalc.updateTaskSLAs(confirmSLA);

            // AC5: record the cancellation for reporting + log the details
            recordSLACancellationMetric(confirmSLA, taskGR, closedDateTime);
            cancelledSLACount++;
        }
    }
}

gs.warn('Task SLA auto-cancel scheduled job complete. Total Task SLAs closed: ' + cancelledSLACount, 'TaskSLAAutoCancel');