Trigger a event separately

Kri
Tera Guru

 

 

 

 

1 REPLY 1

Rajesh Chopade1
Mega Sage

Hi @Kri 

To trigger separate events for table1 and table2, you can modify the script to differentiate between cases from each table and send different events or notifications based on which table the cases are from. 

 

 

var table1 = 'x_nccgp_uts_security_case'; // Replace with your custom table name
var table2 = 'x_nccgp_uts_health_case'; // Replace with your second custom table name
var customerField = 'customer_account'; // Field that stores customer info

// Calculate percentage change
function calculatePercentageChange(oldValue, newValue) {
    if (oldValue === 0) {
        return newValue === 0 ? 0 : 100;
    }
    return ((newValue - oldValue) / oldValue) * 100;
}

// Function to get case counts for a given date and table
function getCaseCountsByDateAndTable(date, tableName) {
    var caseCounts = {};
    var gr = new GlideRecord(tableName);
    gr.addQuery('sys_created_on', '>=', date + ' 00:00:00');
    gr.addQuery('sys_created_on', '<=', date + ' 23:59:59');
    gr.query();
    while (gr.next()) {
        var customer = gr.getValue(customerField);
        caseCounts[customer] = (caseCounts[customer] || 0) + 1;
    }
    return caseCounts;
}

// Calculate date strings for today and yesterday
var today = new GlideDateTime();
var yesterday = new GlideDateTime();
yesterday.addDaysUTC(-1);

var todayDateStr = today.getLocalDate().toString();
var yesterdayDateStr = yesterday.getLocalDate().toString();

// Get case counts for today and yesterday for both tables
var todayCountsTable1 = getCaseCountsByDateAndTable(todayDateStr, table1);
var yesterdayCountsTable1 = getCaseCountsByDateAndTable(yesterdayDateStr, table1);
var todayCountsTable2 = getCaseCountsByDateAndTable(todayDateStr, table2);
var yesterdayCountsTable2 = getCaseCountsByDateAndTable(yesterdayDateStr, table2);

// Compare and notify if drop is 40% or more for table1
for (var customer in yesterdayCountsTable1) {
    var yesterdayCount = yesterdayCountsTable1[customer];
    var todayCount = todayCountsTable1[customer] || 0;
    var percentageDrop = calculatePercentageChange(yesterdayCount, todayCount);

    if (percentageDrop <= -40) {
        // Construct notification message for table1
        var message1 = 'Significant drop in security cases for customer ' + customer + ': ' +
            'Yesterday: ' + yesterdayCount + ', Today: ' + todayCount +
            ' (' + Math.abs(percentageDrop.toFixed(2)) + '% drop)';

        // Trigger notification for table1 (use an event or direct email as per your setup)
        gs.eventQueue('security_case_drop', null, message1, '');
    }
}

// Compare and notify if drop is 40% or more for table2
for (var customer in yesterdayCountsTable2) {
    var yesterdayCount = yesterdayCountsTable2[customer];
    var todayCount = todayCountsTable2[customer] || 0;
    var percentageDrop = calculatePercentageChange(yesterdayCount, todayCount);

    if (percentageDrop <= -40) {
        // Construct notification message for table2
        var message2 = 'Significant drop in health cases for customer ' + customer + ': ' +
            'Yesterday: ' + yesterdayCount + ', Today: ' + todayCount +
            ' (' + Math.abs(percentageDrop.toFixed(2)) + '% drop)';

        // Trigger notification for table2 (use an event or direct email as per your setup)
        gs.eventQueue('health_case_drop', null, message2, '');
    }
}

 

 

 

I hope my answer helps you to resolve your issue, if yes please mark my answer helpful & correct.

thank you

rajesh