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.

Handle Email test cases

Utkarsha
Tera Contributor

Hello Everyone,

I am working on scheduled script using which I need to configure it in such a way that it can handle below scenarios

Case 1

Table : Company
No of days : 2
Users to be notified :user1

Case 2

Table :Exception
No of days 5
users to be notified : user 2
No of days : 1
users to be notified : user3,user4,user5
I am using enclosed scheduled script...could anyone suggest where should I make changes so that it'll pass all the test cases?
Any kind of help is greatly appreciated here,

Scheduled Script:
function getFreshness(tableName) {
    var lastUpdated = new global.GlideQuery(tableName)
        .limit(1)
        .orderByDesc('sys_updated_on')
        .select('sys_updated_on')
        .reduce(function (_, record) {
            return record.sys_updated_on;
        }, '');


    var today = new GlideDateTime(),
        daysAgo = GlideDateTime
            .subtract(new GlideDateTime(lastUpdated), today)
            .getDisplayValue();

    return {
        tableName: tableName,
        lastUpdated: lastUpdated,
        daysAgo: daysAgo

    };
}
// Get the system property
var jsonString = gs.getProperty('track_integration_failures');

// Parse the JSON
var parsedJson = JSON.parse(jsonString);

// Access the values
var tableNames = parsedJson.table;
var operations = parsedJson.operations;
var userToNotify = parsedJson['user to notify'].toString();
var numberOfDays = parsedJson['number of days'];
//  Loop through each case
// parsedJson.forEach(function (caseInfo) {
// var tableNames = caseInfo.table;
// var userToNotify = caseInfo['user to notify'].toString();
// var numberOfDays = caseInfo['number of days'];

// Log the values
// gs.info('Table: ' + tableNames);

var unUsedTables = [];
var message = "There were no interactions in the tables for the last " + numberOfDays;
var query = '';
if (operations == 'both') {
    query = 'sys_updated_onNOTONOne year ago@javascript:gs.beginningOfOneYearAgo()@javascript:gs.endOfOneYearAgo()^ORsys_created_onNOTONOne year ago@javascript:gs.beginningOfOneYearAgo()@javascript:gs.endOfOneYearAgo()'
} else if (operations == 'insert') {
    query = 'sys_created_onNOTONOne year ago@javascript:gs.beginningOfOneYearAgo()@javascript:gs.endOfOneYearAgo()'
} else if (operations == 'update') {
    query = 'sys_updated_onNOTONOne year ago@javascript:gs.beginningOfOneYearAgo()@javascript:gs.endOfOneYearAgo()'
}
var tableInfo = [];
tableNames.forEach(function (tableName) {
    var gr = new GlideRecord(tableName);
    gr.addEncodedQuery(query);
    gr.query();

    if (gr.hasNext()) {
        tableInfo.push(getFreshness(tableName));
        //***********************

        unUsedTables.push(tableName.toString());
    }
});
var parm1 = { 'unUsedTables': unUsedTables, 'tableInfo': tableInfo, 'numberOfDays': numberOfDays };
gs.eventQueue('track_insert_or_update', null, JSON.stringify(parm1), userToNotify);
2 REPLIES 2

Subhashis Ratna
Tera Guru

Hi @Utkarsha  

You can achieve the same functionality by using a traditional for loop to iterate over the cases defined in your JSON configuration and we can eliminate unnecessary variable declarations and simplify the conditionals.

Here i have optimize the code :

SubhashisRatna_0-1712831330561.png

 

for (var i = 0; i < parsedJson.length; i++) {
    var caseInfo = parsedJson[i];
    var tableNames = caseInfo.table;
    var userToNotify = caseInfo['user to notify'].toString();
    var numberOfDays = caseInfo['number of days'];
    
    gs.info('Table: ' + tableNames);

    var unUsedTables = [];
    var message = "There were no interactions in the tables for the last " + numberOfDays + " days";
    
    for (var j = 0; j < tableNames.length; j++) {
        var tableName = tableNames[j];
        var operations = caseInfo.operations;
        var query = '';
        
        if (operations === 'both') {
            query = 'sys_updated_onNOTON' + numberOfDays + 'daysago@javascript^ORsys_created_onNOTON' + numberOfDays + 'daysago@javascript';
        } else {
            query = (operations === 'insert') ? 'sys_created_onNOTON' : 'sys_updated_onNOTON';
            query += numberOfDays + 'daysago@javascript';
        }
        
        var grr = new GlideRecord(tableName);
        grr.addEncodedQuery(query);
        grr.query();

        if (!grr.hasNext()) {
            unUsedTables.push(tableName);
        }
    }
    
    var parm1 = { 'unUsedTables': unUsedTables, 'message': message };
    gs.eventQueue('track_insert_or_update', null, JSON.stringify(parm1), userToNotify);
}

If this solution resolves your query, kindly mark it as the accepted solution and give it a thumbs up.

Thanks,
Subhashis Ratna




Hello @Subhashis Ratna ,

Thank you for responding....I tried the optimized code which you shared, but it is not at all triggering emails in the system.. could you please take a look at the code and let me know where am i getting wrong?

Thank you,

// Get the system property
var jsonString = gs.getProperty('track_integration_failures');

// Parse the JSON
var parsedJson = JSON.parse(jsonString);
for (var i = 0; i < parsedJson.length; i++) {
    var caseInfo = parsedJson[i];
    var tableNames = caseInfo.table;
    var userToNotify = caseInfo['user to notify'].toString();
    var numberOfDays = caseInfo['number of days'];

    gs.info('Table Names in logs: ' + tableNames);

    var unUsedTables = [];
    var message = "There were no interactions in the tables for the last " + numberOfDays + " days";

    for (var j = 0; j < tableNames.length; j++) {
        var tableName = tableNames[j];
        var operations = caseInfo.operations;
        var query = '';

        if (operations === 'both') {
            query = 'sys_updated_onNOTON' + numberOfDays + 'daysago@javascript^ORsys_created_onNOTON' + numberOfDays + 'daysago@javascript';
        } else {
            query = (operations === 'insert') ? 'sys_created_onNOTON' : 'sys_updated_onNOTON';
            query += numberOfDays + 'daysago@javascript';
        }

        var grr = new GlideRecord(tableName);
        grr.addEncodedQuery(query);
        grr.query();

        if (!grr.hasNext()) {
            unUsedTables.push(tableName);
        }
    }

    var parm1 = { 'unUsedTables': unUsedTables, 'message': message };
    gs.eventQueue('track_insert_or_update', null, JSON.stringify(parm1), userToNotify);
}