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

@Ankur Bawiskar Thanks a lot, it is working as expected.

 

 

@Vasavi O 

I believe I have already answered your original question and subsequent question.

Please mark both of my answers as correct as you can now mark multiple responses as correct.

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

@Vasavi O 

you can also enhance the script to check if the record is for variable or variable set

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

GlideFather
Tera Patron

Is it possible that there is more approvals? with that creating the approver, reviewer fields again

———
/* If my response wasn’t a total disaster ↙️ drop a Kudos or Accept as Solution ↘️ Cheers! */