How to pull "Requested Item Summary" field values of sysapproval_approver to notification body?

sharsha
Mega Contributor

How to pull all related field values of "Requested Item Summary"  of a sysapproval_approver record to a notification body using Email template in Servicenow?
Please see attached and advise if you've any idea!?

3 ACCEPTED SOLUTIONS

vaishali231
Kilo Sage

Hey @sharsha 

Try this :
Step 1: Create a Notification Email Script

Create a new Email Script (for example, RITM_Variable_Summary) 

code:

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

var ritm = current.sysapproval.getRefRecord();

if (!ritm || !ritm.isValidRecord())
return;

template.print("<h3>Requested Item Summary</h3>");
template.print("<table border='1' cellpadding='5' cellspacing='0' style='border-collapse:collapse;width:100%;'>");
template.print("<tr style='background:#f2f2f2;'>");
template.print("<th align='left'>Question</th>");
template.print("<th align='left'>Response</th>");
template.print("</tr>");

var variables = ritm.variables.getElements();

for (var i = 0; i < variables.length; i++) {

var variable = variables[i];

template.print("<tr>");
template.print("<td>" + variable.getQuestion().getLabel() + "</td>");
template.print("<td>" + variable.getDisplayValue() + "</td>");
template.print("</tr>");
}

template.print("</table>");

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

Step 2: Call the Mail Script in the Notification
In your Approval Notification (Table: sysapproval_approver), add the following to the Message HTML:
Requested Item Summary

${mail_script:RITM_Variable_Summary}

*********************************************************************************************************************************
If this response helps, please mark it as Accept as Solution and Helpful.
Doing so helps others in the community and encourages me to keep contributing.
Regards
Vaishali Singh
Servicenow Developer
Linkedin - https://www.linkedin.com/in/vaishali-singh-2273361bb

 

 

View solution in original post

Ankur Bawiskar
Tera Patron

@sharsha 

You need to use email script in your email body and include it in email

Mail script: print_cat_item_request_submitted

Script

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

// Add your code here

template.print('Requested Item Summary: <br/>');

var ritm = new GlideRecord('sc_req_item');
ritm.get('request', current.sysapproval);

var variables = ritm.variables.getElements();
for (var i=0;i<variables.length;i++) {
var question = variables[i].getQuestion();
var label = question.getLabel();
var value = question.getDisplayValue();
if(label != '' && value !=''){
template.space(4);
template.print(' ' + label + " = " + value + "<br/>");
}
}

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

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

View solution in original post

Tanushree Maiti
Tera Patron

Hi @sharsha 

 

To retrieve the fields and variables of a Requested Item (RITM) from a sysapproval_approver record in an email notification, use an Email Script.

Note: Standard dot-walking (for example, ${sysapproval.variable_name}) does not reliably display catalog variables, especially multi-line variables or complete variable summaries.

 

Refer: https://www.servicenow.com/community/itsm-forum/requested-item-variables-in-email-body-for-approval/...

https://www.servicenow.com/community/developer-forum/email-template-for-approval-grab-variable-for-e...

 

You can try this sample email script.

1: Create an Email Script

Navigate to System Policy > Email > Notification Email Scripts and create a new script.

  • Name: ritm_summary_details

 

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

    var ritmSysId = current.getValue('sysapproval');

    if (ritmSysId) {

        var ritmGR = new GlideRecord('sc_req_item');

        if (ritmGR.get(ritmSysId)) {           

            template.print('<h3>Requested Item Summary</h3>');

            template.print('<b>Item:</b> ' + ritmGR.cat_item.getDisplayValue() + '<br />');

            template.print('<b>Item Number:</b> ' + ritmGR.getValue('number') + '<br />');

            template.print('<b>Quantity:</b> ' + ritmGR.getValue('quantity') + '<br />');

            template.print('<b>Short Description:</b> ' + ritmGR.getValue('short_description') + '<br /><br />');

             template.print('<b>Options / Variables Selected:</b><br />');

            var variables = ritmGR.variables;

            for (var keyRitm in variables) {

                var varRitm = variables[keyRitm];

                  if (varRitm.getGlideObject().getQuestion().getLabel() != '' && varRitm.getDisplayValue() != '') {

                    template.print('&nbsp;&nbsp;&bull;&nbsp;' + varRitm.getGlideObject().getQuestion().getLabel() + ': ' + varRitm.getDisplayValue() + '<br />');

                }

            }

        }

    }

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

 

2: Call the Script in Your Email Body/Template

  • Open the targeted Email Template or Notification record configured on the sysapproval_approver table.
  • Insert the script call inside the Message HTML body

 

Dear ${approver},

 

An approval request has been generated for your review.

 

${mail_script:ritm_summary_details}

 

Click here to view the approval: ${URI_REF}

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti

View solution in original post

9 REPLIES 9

vaishali231
Kilo Sage

Hey @sharsha 

Try this :
Step 1: Create a Notification Email Script

Create a new Email Script (for example, RITM_Variable_Summary) 

code:

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

var ritm = current.sysapproval.getRefRecord();

if (!ritm || !ritm.isValidRecord())
return;

template.print("<h3>Requested Item Summary</h3>");
template.print("<table border='1' cellpadding='5' cellspacing='0' style='border-collapse:collapse;width:100%;'>");
template.print("<tr style='background:#f2f2f2;'>");
template.print("<th align='left'>Question</th>");
template.print("<th align='left'>Response</th>");
template.print("</tr>");

var variables = ritm.variables.getElements();

for (var i = 0; i < variables.length; i++) {

var variable = variables[i];

template.print("<tr>");
template.print("<td>" + variable.getQuestion().getLabel() + "</td>");
template.print("<td>" + variable.getDisplayValue() + "</td>");
template.print("</tr>");
}

template.print("</table>");

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

Step 2: Call the Mail Script in the Notification
In your Approval Notification (Table: sysapproval_approver), add the following to the Message HTML:
Requested Item Summary

${mail_script:RITM_Variable_Summary}

*********************************************************************************************************************************
If this response helps, please mark it as Accept as Solution and Helpful.
Doing so helps others in the community and encourages me to keep contributing.
Regards
Vaishali Singh
Servicenow Developer
Linkedin - https://www.linkedin.com/in/vaishali-singh-2273361bb

 

 

Hey @sharsha 

Hope you are doing well.

Did my previous reply answer your question?

If it was helpful, please mark it as correct ✓ and close the thread . This will help other readers find the solution more easily.

 

Thankyou & Regards

Vaishali Singh

Servicenow Developer
Linkedin - https://www.linkedin.com/in/vaishali-singh-2273361bb







Ankur Bawiskar
Tera Patron

@sharsha 

You need to use email script in your email body and include it in email

Mail script: print_cat_item_request_submitted

Script

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

// Add your code here

template.print('Requested Item Summary: <br/>');

var ritm = new GlideRecord('sc_req_item');
ritm.get('request', current.sysapproval);

var variables = ritm.variables.getElements();
for (var i=0;i<variables.length;i++) {
var question = variables[i].getQuestion();
var label = question.getLabel();
var value = question.getDisplayValue();
if(label != '' && value !=''){
template.space(4);
template.print(' ' + label + " = " + value + "<br/>");
}
}

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

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

@sharsha 

Just following up to see whether my suggested solution worked for you.

If you're still facing any issues, I'd be happy to help further.

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

Tanushree Maiti
Tera Patron

Hi @sharsha 

 

To retrieve the fields and variables of a Requested Item (RITM) from a sysapproval_approver record in an email notification, use an Email Script.

Note: Standard dot-walking (for example, ${sysapproval.variable_name}) does not reliably display catalog variables, especially multi-line variables or complete variable summaries.

 

Refer: https://www.servicenow.com/community/itsm-forum/requested-item-variables-in-email-body-for-approval/...

https://www.servicenow.com/community/developer-forum/email-template-for-approval-grab-variable-for-e...

 

You can try this sample email script.

1: Create an Email Script

Navigate to System Policy > Email > Notification Email Scripts and create a new script.

  • Name: ritm_summary_details

 

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

    var ritmSysId = current.getValue('sysapproval');

    if (ritmSysId) {

        var ritmGR = new GlideRecord('sc_req_item');

        if (ritmGR.get(ritmSysId)) {           

            template.print('<h3>Requested Item Summary</h3>');

            template.print('<b>Item:</b> ' + ritmGR.cat_item.getDisplayValue() + '<br />');

            template.print('<b>Item Number:</b> ' + ritmGR.getValue('number') + '<br />');

            template.print('<b>Quantity:</b> ' + ritmGR.getValue('quantity') + '<br />');

            template.print('<b>Short Description:</b> ' + ritmGR.getValue('short_description') + '<br /><br />');

             template.print('<b>Options / Variables Selected:</b><br />');

            var variables = ritmGR.variables;

            for (var keyRitm in variables) {

                var varRitm = variables[keyRitm];

                  if (varRitm.getGlideObject().getQuestion().getLabel() != '' && varRitm.getDisplayValue() != '') {

                    template.print('&nbsp;&nbsp;&bull;&nbsp;' + varRitm.getGlideObject().getQuestion().getLabel() + ': ' + varRitm.getDisplayValue() + '<br />');

                }

            }

        }

    }

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

 

2: Call the Script in Your Email Body/Template

  • Open the targeted Email Template or Notification record configured on the sysapproval_approver table.
  • Insert the script call inside the Message HTML body

 

Dear ${approver},

 

An approval request has been generated for your review.

 

${mail_script:ritm_summary_details}

 

Click here to view the approval: ${URI_REF}

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti