Notification email script is not populating email body when sent to some users.

Sean1232
Tera Contributor

Having issues where this email script is not populating the body of the email for some users. This is only happening in our production environment. Emails are beeing sent to all of the related users as intended but some users will have the body populated including links to the expiring articles and others will have a blank email body. 

 

Email notification is fired from an event that is triggered by a scheduled job that runs once a month. The scheduled job will check if there are expiring articles for the next month and fire the event.

 

Email Script:

 

(function runMailScript(current, template, email, email_action, event) {

    var author = event.parm1;
    var gr = new GlideRecord('kb_knowledge');
    gr.addEncodedQuery('workflow_stateINpublished^valid_toONNext month@javascript:gs.beginningOfNextMonth()@javascript:gs.endOfNextMonth()');
    gr.addQuery('author', '=', author); // Filter articles by the current user
    gr.orderBy('valid_to'); // Sort articles by valid_to date
    gr.query();
    var articles = []; // Store articles

    while (gr.next()) {
        articles.push({
            title: gr.short_description.toString(),
            number: gr.number.toString(),
            validTo: gr.valid_to.toString(),
            sysId: gr.sys_id.toString()
        });
    }

    // Construct the base URL based on the environment
    var servletURI = gs.getProperty("glide.servlet.uri");
    var instance = "";
    if (servletURI.includes("dev")) {
        instance = "https://dev.service-now.com/";
    } else if (servletURI.includes("test")) {
        instance = "https://test.service-now.com/";
    } else {
        instance = "https://prod.com/";
    }

    // Send email if there are expiring articles
    if (articles.length > 0) {
        var emailContent = {};
        emailContent.templateContent =
            "Hi," +
            "<br><br>";

        if (articles.length > 10) {
            /*emailContent.templateContent +=
                "Click the following link to review articles that are expiring next month:<br><br>" + "<a href='" + instance + "kb_knowledge_list.do?sysparm_query=workflow_stateINpublished^valid_toONNext month@javascript&colon;gs.beginningOfNextMonth()@javascript&colon;gs.endOfNextMonth()^author=" + author + "' target='_blank'>My Expiring Articles</a><br>";*/
            emailContent.templateContent +=
                "Click the following link to review articles that are expiring next month:<br><br>" + "<a href='" + instance + "kb_knowledge_list.do?sysparm_query=workflow_stateINpublished%5Evalid_toONNext%20month@javascript&colon;gs.beginningOfNextMonth%28%29@javascript&colon;gs.endOfNextMonth%28%29%5Eauthor=" + author + "' target='_blank'>My Expiring Articles</a><br>";

        } else {
            emailContent.templateContent +=
                "Please review the following article(s) that are expiring next month:<br><br>";
            // Start of table with lines between header and contents
            emailContent.templateContent += "<table style='width: 100%; border-collapse: collapse;'>";
            // Table headers with lines between columns
            emailContent.templateContent +=
                "<tr>" +
                "<th style='text-align: center; padding: 8px; border-bottom: 1px solid black;'>Article Number</th>" +
                "<th style='text-align: center; padding: 8px; border-bottom: 1px solid black; border-left: 1px solid black;'>Article Title</th>" +
                "<th style='text-align: center; padding: 8px; border-bottom: 1px solid black; border-left: 1px solid black;'>Valid To</th>" +
                "</tr>";

            for (var i = 0; i < articles.length; i++) {
                var articleLink = instance + "kb_knowledge.do?sys_id=" + articles[i].sysId;
                emailContent.templateContent +=
                    "<tr>" +
                    "<td style='text-align: center; padding: 8px;'>" +
                    "<a href='" + articleLink + "' target='_blank'>" + articles[i].number + "</a></td>" +
                    "<td style='border-left: 1px solid black; padding: 8px;'>" + articles[i].title + "</td>" +
                    "<td style='text-align: center; padding: 8px; border-left: 1px solid black;'>" + articles[i].validTo + "</td>" +
                    "</tr>";
            }

            // End of table with lines between columns
            emailContent.templateContent += "</table>";
        }

        emailContent.templateContent +=
            "<br>" +
            "If you have access, please update the article(s) accordingly if you wish for them to remain published.<br>" +
            "<br>If you do not have access, please submit a case via the <a href='/=' target='_blank'>Knowledge Article Request</a> in blank to provide any updates or confirmation that the article(s) should be republished.<br>";

        emailContent.templateContent +=
            "<br>Otherwise, the article(s) listed above will expire next month.";

        template.print(emailContent.templateContent);
    }

})(current, template, email, email_action, event);

 

 

1 REPLY 1

Tony Chatfield1
Kilo Patron

Hi, if your script is working for some users and not others then perhaps the issue is that a scripted variable or dependency is not valid for the users it fails for?
I would suggest you start by adding logging\debugging to your code.

First ensure the script is triggered, then that the glide query returns results for your impacted user(s).

Then validate articles/articles.length to confirm that the expected statements conditions are met and your body is populated as expected.