- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2025 11:23 AM
Hello,
I'm here after following the ticket Solved: Approval Email Display RITM Ticket Variables - Fol... - ServiceNow Community
1. But this script is not following the UI catalog policies to display what the user had submitted within the ESC to display email variables. I think it's showing whatever variable is not empty during the submission of the catalog form. for example, if variable "A" of checkbox type is not checked still the value is showing "A: false " in the approval email.
2. Not displaying label type variables. Not displaying a file which is attached in the attachment variable.
Could anyone please help in figuring out the solution for the above issues.
email script used:
(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */
email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */
event) {
var item_sys_id;
var tableName = current.getTableName();
if(tableName == 'sysapproval_approver'){
item_sys_id = current.sysapproval;
} else if (tableName == 'sc_task'){
item_sys_id = current.request_item.sys_id;
} else if(tableName == 'sc_req_item'){
item_sys_id = current.sys_id;
} else{
item_sys_id ='';
}
//gs.log("SYS ID : "+item_sys_id);
var item = new GlideRecord("sc_req_item");
item.addQuery("sys_id", item_sys_id);
item.query();
while (item.next()) {
template.print("<div style='font-size:12pt;font-family:Arial, Helvetica, sans-serif;'><span><b>Options</b>:</span><br />");
var keys = [];
var set = new GlideappVariablePoolQuestionSet();
set.setRequestID(item.sys_id);
set.load();
var vs = set.getFlatQuestions();
for (var i = 0; i < vs.size(); i++) {
if (vs.get(i).getLabel() != '') { //This displays all of the variables (answered/unanswered)
var val = vs.get(i).getDisplayValue();
if (val != '') {
template.space(4);
template.print(vs.get(i).getLabel() + " : " + val + "<br />");
}
}
}
template.print("</div>");
}
// Add your code here
})(current, template, email, email_action, event);
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2025 05:57 AM
really difficult to determine in email script whether variable was hidden on form during catalog submission
So it's not possible
Doesn't make sense in showing Label type variable as it doesn't hold information
Doesn't logically fit to show the attachment variable info in email
You can enhance the script to show only non empty variables and checkbox with true value
(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */
email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */
event) {
var item_sys_id;
var tableName = current.getTableName();
if(tableName == 'sysapproval_approver'){
item_sys_id = current.sysapproval;
} else if (tableName == 'sc_task'){
item_sys_id = current.request_item.sys_id;
} else if(tableName == 'sc_req_item'){
item_sys_id = current.sys_id;
} else{
item_sys_id ='';
}
//gs.log("SYS ID : "+item_sys_id);
var item = new GlideRecord("sc_req_item");
item.addQuery("sys_id", item_sys_id);
item.query();
while (item.next()) {
template.print("<div style='font-size:12pt;font-family:Arial, Helvetica, sans-serif;'><span><b>Options</b>:</span><br />");
var keys = [];
var set = new GlideappVariablePoolQuestionSet();
set.setRequestID(item.sys_id);
set.load();
var vs = set.getFlatQuestions();
for (var i = 0; i < vs.size(); i++) {
if (vs.get(i).getLabel() != '') { //This displays all of the variables (answered/unanswered)
var val = vs.get(i).getDisplayValue();
if (val != '' && val != 'false') {
template.space(4);
template.print(vs.get(i).getLabel() + " : " + val + "<br />");
}
}
}
template.print("</div>");
}
// Add your code here
})(current, template, email, email_action, event);
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2025 11:38 PM
for adding custom file in email just when it's sent check this link where I shared solution and you can enhance it. Please ensure in the below link we are picking file from record but you will have to pick it from variable
Attachment to the notification
sharing here again the logic
create after insert BR on sys_email with condition as this
You change the table as sc_req_item
BR script
(function executeRule(current, previous /*null when async*/) {
var s_Attach = new GlideRecord('sys_attachment');
s_Attach.addEncodedQuery('table_sys_id=' + current.instance);
s_Attach.addQuery('table_name', 'ZZ_YYsc_req_item');
s_Attach.query();
if(s_Attach.next()){
var rec = new GlideRecord('sys_email_attachment');
rec.initialize();
rec.attachment = s_Attach.sys_id;
rec.file_name = s_Attach.file_name;
rec.source = 'notification';
rec.content_disposition = 'attachment';
rec.email = current.sys_id;
rec.insert();
}
})(current, previous);
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2025 11:48 PM
I hope I have provided enough answer to your question and you can take it further from here based on your requirement and your skills.
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader