Inbound action script is not handling exceptions

GAyathri33
Tera Contributor

Hi Team,

 

Its a bit high priority and provide your solution.

 Written inbound action, without exceptions code is working fine, but if I write exceptions and evenQueue, it is not handling the exceptions and inbound action is not processed.  Please check below code of mine and provide your solutions as soon as possible.

******Code*************

var excp = "";
var existingUsers = [];
var processedUsers = [];

if (email.subject.indexOf("PingCo NOC User List for Visibility Group Automation") !== -1) {
    var userGR = new GlideRecord("sys_user");
    userGR.addQuery("company.name", "TCAP PingCo");
    userGR.query();
    while (userGR.next()) {
        existingUsers.push(userGR.user_name.toString());
    }
    gs.log("Existing PingCo users retrieved");


    var emailGR = new GlideRecord("sys_email");
    emailGR.addQuery("subject", "LIKE", "%PingCo NOC User List for Visibility Group Automation%");
    emailGR.orderByDesc("sys_created_on");
    emailGR.setLimit(1);
    emailGR.query();

    if (emailGR.next()) {
        gs.log("PingCo email processed");
        var emailSysId = emailGR.getUniqueValue();
        gs.log("PingCo email sys_id" + emailSysId);
        var attachmentGR = new GlideRecord("sys_attachment");
        attachmentGR.addQuery("table_name", "sys_email");
        attachmentGR.addQuery("table_sys_id", emailSysId);
        // attachmentGR.addQuery("content_type", "text/csv");
        attachmentGR.orderByDesc("sys_created_on");
        attachmentGR.query();
        gs.log("PingCo attachemtn processed");
        if (attachmentGR.next()) {
            var gsa = new GlideSysAttachment();
            var attachmentData = gsa.getBytes(attachmentGR);
            var attachment = String(Packages.java.lang.String(attachmentData));
            var lines = attachment.split('\n');
            for (var i = 0; i < lines.length; i++) {
                var userId = lines[i].trim();
                // }
                if (userId && userId.indexOf("ID") === -1) {
                    gs.log("PingCo attachment read");
                    processedUsers.push(userId);
                    if (existingUsers.includes(userId)) {
                        var activeUser = new GlideRecord("sys_user");
                        activeUser.addQuery("user_name", userId);
                        activeUser.query();
                        if (activeUser.next()) {
                            activeUser.active = true;
                            activeUser.update();
                            gs.log("PingCo Reactivated user: " + userId);
                        }
                    } else {
                        var newUser = new GlideRecord("sys_user");
                        newUser.initialize();
                        newUser.user_name = userId;
                        newUser.company.setDisplayValue("TCAP PingCo");
                        newUser.active = true;
                        newUser.insert();
                        gs.log("PingCo Created new user: " + userId);
                    }
                }
            }

            for (var j = 0; j < existingUsers.length; j++) {
                var existingUserId = existingUsers[j];
                if (!processedUsers.includes(existingUserId)) {
                    var inactiveUser = new GlideRecord("sys_user");
                    inactiveUser.addQuery("user_name", existingUserId);
                    inactiveUser.query();
                    if (inactiveUser.next()) {
                        inactiveUser.active = false;
                        inactiveUser.update();
                        gs.log("PingCo Marked inactive: " + existingUserId);
                    }
                }
            }
        } else {
            excp += "No CSV attachment found in the email.\n";
        }
    } else {
        excp += "No matching email found.\n";
    }
} else {
    excp += "Email Subject is Wrong. \n";
}
gs.eventQueue("tcap.error", current, email.subject, '');

 

************end************** 

6 REPLIES 6

AshishKM
Kilo Patron
Kilo Patron

Hi ,

Please re-share complete code with exception applied and also did you check the email log details, any catch there.

 

-Thanks,
AshishKM


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

sundaram080713
Tera Expert

 

Hello ,

 

you may consider is wrapping your critical logic in try-catch blocks. If an unexpected error occurs, the catch block can log and append the error to excp, ensuring that your code doesn’t fail silently. For example:

 

 

try {

// Your existing logic (reading email, attachments, etc.)

} catch (error) {

excp += "Error encountered: " + error.message + "\n";

}

 

 

Then, after your processing, you can decide whether to queue an error event based on whether excp has any text.

 

 please mark it helpfull if it's help you 
Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

Thank you !!

sundaram

https://www.linkedin.com/in/sundaram-s-454718156/

GAyathri33
Tera Contributor
var excp = "";
var existingUsers = [];
var processedUsers = [];

if (email.subject.indexOf("PingCo NOC User List for Visibility Group Automation") !== -1) {
    var userGR = new GlideRecord("sys_user");
    userGR.addQuery("company.name", "TCAP PingCo");
    userGR.query();
    while (userGR.next()) {
        existingUsers.push(userGR.user_name.toString());
    }
    gs.log("Existing PingCo users retrieved");


    var emailGR = new GlideRecord("sys_email");
    emailGR.addQuery("subject", "LIKE", "%PingCo NOC User List for Visibility Group Automation%");
    emailGR.orderByDesc("sys_created_on");
    emailGR.setLimit(1);
    emailGR.query();

    if (emailGR.next()) {
        gs.log("PingCo email processed");
        var emailSysId = emailGR.getUniqueValue();
        gs.log("PingCo email sys_id" + emailSysId);
        var attachmentGR = new GlideRecord("sys_attachment");
        attachmentGR.addQuery("table_name", "sys_email");
        attachmentGR.addQuery("table_sys_id", emailSysId);
        // attachmentGR.addQuery("content_type", "text/csv");
        // attachmentGR.addQuery("sys_id", "959b91a01b6ca2508e50ed38b04bcbcd");
        attachmentGR.orderByDesc("sys_created_on");
        attachmentGR.query();
        gs.log("PingCo attachemtn processed");
        if (attachmentGR.next()) {
            var gsa = new GlideSysAttachment();
            var attachmentData = gsa.getBytes(attachmentGR);
            var attachment = String(Packages.java.lang.String(attachmentData));
            var lines = attachment.split('\n');
            for (var i = 0; i < lines.length; i++) {
                var userId = lines[i].trim();
                // }
                if (userId && userId.indexOf("ID") === -1) {
                    gs.log("PingCo attachment read");
                    processedUsers.push(userId);
                    if (existingUsers.includes(userId)) {
                        var activeUser = new GlideRecord("sys_user");
                        activeUser.addQuery("user_name", userId);
                        activeUser.query();
                        if (activeUser.next()) {
                            activeUser.active = true;
                            activeUser.update();
                            gs.log("PingCo Reactivated user: " + userId);
                        }
                    } else {
                        var newUser = new GlideRecord("sys_user");
                        newUser.initialize();
                        newUser.user_name = userId;
                        newUser.company.setDisplayValue("TCAP PingCo");
                        newUser.active = true;
                        newUser.insert();
                        gs.log("PingCo Created new user: " + userId);
                    }
                }
            }

            for (var j = 0; j < existingUsers.length; j++) {
                var existingUserId = existingUsers[j];
                if (!processedUsers.includes(existingUserId)) {
                    var inactiveUser = new GlideRecord("sys_user");
                    inactiveUser.addQuery("user_name", existingUserId);
                    inactiveUser.query();
                    if (inactiveUser.next()) {
                        inactiveUser.active = false;
                        inactiveUser.update();
                        gs.log("PingCo Marked inactive: " + existingUserId);
                    }
                }
            }
        } else {
            excp += "No CSV attachment found in the email.\n";
        }
    } else {
        excp += "No matching email found.\n";
    }
} else {
    excp += "Email Subject is Wrong. \n";
}
gs.eventQueue("tcap.error", current, email.subject, '');
 

GAyathri33
Tera Contributor

Fetching error through ${even.parm1} in notification body text