- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2017 07:48 AM
Hi everyone,
I am a ServiceNow rookie so there is probably an easy solution to what I am about to ask.
We have an email template that sends information based on the script below. I would like to add the variables/questions at the RITM level to be included in the email. I would like to create a query to get return the variables/questions, and loop through each and dynamically add the entries to the email. Since the variables are different for various catalog items, I don't want to hardcode variable names.
Thanks,
Shane
-------------------------------
Click here to view Request:
Number:
Due date:
Opened:
Approval:
<mail_script>
template.print("<p></p>Requested items:\n");
var gr = new GlideRecord("sc_req_item");
gr.addQuery("request", current.sys_id);
gr.query();
while(gr.next()) {
var stage = gr.stage.getDisplayValue();
if (JSUtil.nil(stage))
stage = gr.stage.getChoiceValue();
template.print(gr.number + ": " + gr.cat_item.getDisplayValue() + ", Stage: " + stage + "\n");
}
</mail_script>
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2017 01:54 PM
Got it going. I found another post that referenced how to loop through variables. Updated script below.
---------------------
Click here to view Request: ${URI_REF}
Number: ${number}
Due date: ${due_date}
Opened: ${opened_at}
Approval: ${approval}
<mail_script>
template.print("<p></p>Requested items:\n");
var gr = new GlideRecord("sc_req_item");
gr.addQuery("request", current.sys_id);
gr.query();
while(gr.next()) {
var stage = gr.stage.getDisplayValue();
var misc = gr.variable_pool.alt_poc;
if (JSUtil.nil(stage))
stage = gr.stage.getChoiceValue();
template.print(gr.number + ": " + gr.cat_item.getDisplayValue() + ", Stage: " + stage + "</br>");
for (key in gr.variables) {
var v = gr.variables[key];
if(v.getGlideObject().getQuestion().getLabel() != '') {
template.space(4);
template.print(' ' + v.getGlideObject().getQuestion().getLabel() + " = " + v.getDisplayValue() + "\n");
}
}
}
</mail_script>
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2017 07:52 AM
Hi Jeromy,
This should work
<mail_script>
var item = new GlideRecord("sc_req_item");
item.addQuery("request", current.sys_id);
item.query();
while(item.next()) {
var catalogItem = item.number + ': ' + item.cat_item.getDisplayValue();
var misc = item.variable_pool.alt_poc;
template.print(catalogItem + "<br/> Field: " + misc);
}
</mail_script>
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2017 01:34 PM
Thanks for the reply. I saw the community post where that script applied, but I don't have a variable called of alt_pos. I am wanting to look through all variables for the item and display each, not just specific ones.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2017 01:54 PM
Got it going. I found another post that referenced how to loop through variables. Updated script below.
---------------------
Click here to view Request: ${URI_REF}
Number: ${number}
Due date: ${due_date}
Opened: ${opened_at}
Approval: ${approval}
<mail_script>
template.print("<p></p>Requested items:\n");
var gr = new GlideRecord("sc_req_item");
gr.addQuery("request", current.sys_id);
gr.query();
while(gr.next()) {
var stage = gr.stage.getDisplayValue();
var misc = gr.variable_pool.alt_poc;
if (JSUtil.nil(stage))
stage = gr.stage.getChoiceValue();
template.print(gr.number + ": " + gr.cat_item.getDisplayValue() + ", Stage: " + stage + "</br>");
for (key in gr.variables) {
var v = gr.variables[key];
if(v.getGlideObject().getQuestion().getLabel() != '') {
template.space(4);
template.print(' ' + v.getGlideObject().getQuestion().getLabel() + " = " + v.getDisplayValue() + "\n");
}
}
}
</mail_script>