- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2025 02:27 PM
I have an email script I use for RITM and SC_TASK (below). How can I run this on an email notification targeting the [sc_request] table? assuming the request only has 1 RITM.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2025 02:37 PM
If your email script is being used on a notification which is on sc_request table, you would need to get the related requested item in order to get the variables.
Please use and adjust the following as per your requirements:
(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */
email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */
event) {
var grRequest = new GlideRecord("sc_request");
if (grRequest.get(grRequest.sys_id)) {
var grReqItem = new GlideRecord("sc_req_item");
grReqItem.addQuery("request=" + grRequest.sys_id);
grReqItem.query();
if (grReqItem.next()) {
template.print('<span style="font-size: 14pt; font-weight: 600">Request Details</span><br/><br/>');
var variables = grReqItem.variables.getElements();
for (var i = 0; i < variables.length; i++) {
var question = variables[i].getQuestion();
var label = question.getLabel();
var value = question.getDisplayValue();
if (value != '' && value != 'false') {
template.space(4);
template.print(label + ': <span style="font-weight: 600">' + value + '</span><br/>');
}
}
}
}
})(current, template, email, email_action, event);
If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2025 07:31 PM
Hi @Scott29
Try with below email script. Ensure that your notification is running on sc_request table.
(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */
email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */
event) {
// Add your code here
template.print("Summary of Requested items:<br />");
var item = new GlideRecord("sc_req_item");
item.addQuery("request", current.sys_id);
item.query();
while (item.next()) {
template.print(item.number + ": " + item.quantity + " X " + item.cat_item.getDisplayValue() + " at " + item.cat_item.price.getDisplayValue() + " each <br />");
template.print(" Options:<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() != '') {
template.space(4);
template.print(' ' + vs.get(i).getLabel() + " = " + vs.get(i).getDisplayValue() + "<br />");
}
}
}
})(current, template, email, email_action, event);
Thanks and Regards
Amit Verma
Please mark this response as correct and helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2025 09:10 PM
update as this
(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */
email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */
event) {
var ritmRec = new GlideRecord("sc_req_item");
if (ritmRec.get('request', current.sys_id)) {
template.print('<span style="font-size: 14pt; font-weight: 600">Request Details</span><br/><br/>');
var variables = ritmRec.variables.getElements();
for (var i = 0; i < variables.length; i++) {
var question = variables[i].getQuestion();
var label = question.getLabel();
var value = question.getDisplayValue();
if (value != '' && value != 'false') {
template.space(4);
template.print(label + ': <span style="font-weight: 600">' + value + '</span><br/>');
}
}
}
})(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-10-2025 02:37 PM
If your email script is being used on a notification which is on sc_request table, you would need to get the related requested item in order to get the variables.
Please use and adjust the following as per your requirements:
(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */
email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */
event) {
var grRequest = new GlideRecord("sc_request");
if (grRequest.get(grRequest.sys_id)) {
var grReqItem = new GlideRecord("sc_req_item");
grReqItem.addQuery("request=" + grRequest.sys_id);
grReqItem.query();
if (grReqItem.next()) {
template.print('<span style="font-size: 14pt; font-weight: 600">Request Details</span><br/><br/>');
var variables = grReqItem.variables.getElements();
for (var i = 0; i < variables.length; i++) {
var question = variables[i].getQuestion();
var label = question.getLabel();
var value = question.getDisplayValue();
if (value != '' && value != 'false') {
template.space(4);
template.print(label + ': <span style="font-weight: 600">' + value + '</span><br/>');
}
}
}
}
})(current, template, email, email_action, event);
If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2025 07:31 PM
Hi @Scott29
Try with below email script. Ensure that your notification is running on sc_request table.
(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */
email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */
event) {
// Add your code here
template.print("Summary of Requested items:<br />");
var item = new GlideRecord("sc_req_item");
item.addQuery("request", current.sys_id);
item.query();
while (item.next()) {
template.print(item.number + ": " + item.quantity + " X " + item.cat_item.getDisplayValue() + " at " + item.cat_item.price.getDisplayValue() + " each <br />");
template.print(" Options:<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() != '') {
template.space(4);
template.print(' ' + vs.get(i).getLabel() + " = " + vs.get(i).getDisplayValue() + "<br />");
}
}
}
})(current, template, email, email_action, event);
Thanks and Regards
Amit Verma
Please mark this response as correct and helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2025 09:10 PM
update as this
(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */
email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */
event) {
var ritmRec = new GlideRecord("sc_req_item");
if (ritmRec.get('request', current.sys_id)) {
template.print('<span style="font-size: 14pt; font-weight: 600">Request Details</span><br/><br/>');
var variables = ritmRec.variables.getElements();
for (var i = 0; i < variables.length; i++) {
var question = variables[i].getQuestion();
var label = question.getLabel();
var value = question.getDisplayValue();
if (value != '' && value != 'false') {
template.space(4);
template.print(label + ': <span style="font-weight: 600">' + value + '</span><br/>');
}
}
}
})(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