Variable info is displayed incorrectly in approval notification for record producer in scoped app

Vasavi O
Tera Contributor

Hello All ,

 

There is a requirement  to display Variable Information in Approval Notifications for a Record Producer submitted in a Scoped Application, so for that we are using this email script somehow it is working , But  variable details are showing up duplicate.

 

This is the script we use :

 

 

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {
// Add your code here
    if (current.source_table == "x_tmms7_finance_finance_ticket") {
        template.print("Summary of Finance Ticket ");
    }
    template.print('<table border="1">');
    var tbl_obj = new GlideRecord('x_tmms7_finance_finance_ticket');
    tbl_obj.get('sys_id', current.sysapproval);
    for (var key in tbl_obj.variables) {
        template.print("<tr>");
        if (tbl_obj.variables[key]) {
            template.print("<td>" + getQuestionLabel(key) + "</td>\r\n");
            template.print("<td>" + tbl_obj.variables[key].getDisplayValue() + "</td>\r\n");
            template.print("</tr>");
        }
}
    template.print("</table>");
function getQuestionLabel(question) {
        var vara = new GlideRecord('item_option_new');
        vara.addQuery('name', question);
        vara.query();
        if (vara.next()) {
            var question_label = vara.question_text;
            return question_label;
           
        }
    }
})(current, template, email, email_action, event);
 
and these are the variables information that we had in a form while submitting a request.
VasaviO_3-1745390691560.png

 


 

This is the approval notification but there are duplicate variables are showing up, Please guide.

VasaviO_2-1745390521649.png

 


 

1 ACCEPTED SOLUTION

@Vasavi O 

Glad to know that my script worked fine.

To hide false values enhance as this

I added this extra condition in the IF

&& tbl_obj.variables[key].getDisplayValue() != 'false'

(function runMailScript(current, template, email, email_action, event) {
    // Add your code here
    if (current.source_table == "x_tmms7_finance_finance_ticket") {
        template.print("Summary of Finance Ticket ");
    }
    template.print('<table border="1">');

    var tbl_obj = new GlideRecord('x_tmms7_finance_finance_ticket');
    tbl_obj.get('sys_id', current.sysapproval);

    // Use a Set to track processed variables
    var processedVariables = new Set();

    for (var key in tbl_obj.variables) {
        if (tbl_obj.variables[key] && !processedVariables.has(key) && tbl_obj.variables[key].getDisplayValue() != 'false') {
            template.print("<tr>");
            template.print("<td>" + getQuestionLabel(key) + "</td>\r\n");
            template.print("<td>" + tbl_obj.variables[key].getDisplayValue() + "</td>\r\n");
            template.print("</tr>");
            // Mark this variable as processed
            processedVariables.add(key);
        }
    }
    template.print("</table>");

    function getQuestionLabel(question) {
        var vara = new GlideRecord('item_option_new');
        vara.addQuery('name', question);
        vara.query();
        if (vara.next()) {
            return vara.question_text;
        }
        return question;
    }
})(current, template, email, email_action, event);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

8 REPLIES 8

Vasavi O
Tera Contributor

@Ankur Bawiskar Could you Guide me on that ? Thank you.

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Vasavi O 

I don't see any duplicate in your screenshots

but still you can try this

(function runMailScript(current, template, email, email_action, event) {
    // Add your code here
    if (current.source_table == "x_tmms7_finance_finance_ticket") {
        template.print("Summary of Finance Ticket ");
    }
    template.print('<table border="1">');

    var tbl_obj = new GlideRecord('x_tmms7_finance_finance_ticket');
    tbl_obj.get('sys_id', current.sysapproval);

    // Use a Set to track processed variables
    var processedVariables = new Set();

    for (var key in tbl_obj.variables) {
        if (tbl_obj.variables[key] && !processedVariables.has(key)) {
            template.print("<tr>");
            template.print("<td>" + getQuestionLabel(key) + "</td>\r\n");
            template.print("<td>" + tbl_obj.variables[key].getDisplayValue() + "</td>\r\n");
            template.print("</tr>");
            // Mark this variable as processed
            processedVariables.add(key);
        }
    }
    template.print("</table>");

    function getQuestionLabel(question) {
        var vara = new GlideRecord('item_option_new');
        vara.addQuery('name', question);
        vara.query();
        if (vara.next()) {
            return vara.question_text;
        }
        return question;
    }
})(current, template, email, email_action, event);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Thank you for the script , I used that script and it is working , Is it possible to hide the variables which are visible as false [Those are not checked on the form ] ?

 

@Vasavi O 

Glad to know that my script worked fine.

To hide false values enhance as this

I added this extra condition in the IF

&& tbl_obj.variables[key].getDisplayValue() != 'false'

(function runMailScript(current, template, email, email_action, event) {
    // Add your code here
    if (current.source_table == "x_tmms7_finance_finance_ticket") {
        template.print("Summary of Finance Ticket ");
    }
    template.print('<table border="1">');

    var tbl_obj = new GlideRecord('x_tmms7_finance_finance_ticket');
    tbl_obj.get('sys_id', current.sysapproval);

    // Use a Set to track processed variables
    var processedVariables = new Set();

    for (var key in tbl_obj.variables) {
        if (tbl_obj.variables[key] && !processedVariables.has(key) && tbl_obj.variables[key].getDisplayValue() != 'false') {
            template.print("<tr>");
            template.print("<td>" + getQuestionLabel(key) + "</td>\r\n");
            template.print("<td>" + tbl_obj.variables[key].getDisplayValue() + "</td>\r\n");
            template.print("</tr>");
            // Mark this variable as processed
            processedVariables.add(key);
        }
    }
    template.print("</table>");

    function getQuestionLabel(question) {
        var vara = new GlideRecord('item_option_new');
        vara.addQuery('name', question);
        vara.query();
        if (vara.next()) {
            return vara.question_text;
        }
        return question;
    }
})(current, template, email, email_action, event);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader