- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-02-2018 12:07 PM
I'm using the following code to list approvers in a notification email so that users can know who their request is waiting on. However I keep getting "undefined" displaying right under "Email" in the same header box of the table. I feel like this is an easy fix but haven't been able to figure it out yet. I included a screenshot of part of the table it is creating as well so you can see the problem.
(function runMailScript(current, template, email, email_action, event) {
var arr = [];
var gr = new GlideRecord('sysapproval_approver');
gr.addQuery('document_id',current.sys_id);
gr.addQuery('state','requested');
gr.query();
while(gr.next())
{
arr.push(gr.approver.toString());
}
var rows;
for(var i=0;i<arr.length;i++)
{
var usr = new GlideRecord('sys_user');
usr.addQuery('sys_id',arr[i]);
usr.query();
if(usr.next())
{
rows += '<tr>';
rows += '<td>' + usr.first_name + " " + usr.last_name + '</td>';
rows += '<td>' + usr.title + '</td>';
rows += '<td>' + usr.email + '</td>';
rows +='</tr>';
}
}
var data;
data = '<table style="border-collapse: collapse;" border="1" cellpadding="5"><tbody>';
data += '<th>Name</th><th>Position</th><th>Email</th>';
data += rows;
data += '</tbody></table>';
template.print(data);
})(current, template, email, email_action, event);
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-02-2018 01:23 PM
Couple of issues in your script:
1. set rows to an empty string initially. otherwise, it may show undefined like you are seeing.
2. your html is not correct in the "data" string. see my changes below to that as well. You don't have to use the "thead" part I injected, but you need the additional "tr" I put in there to wrap the "th" cells.
this is untested, but should get you what you need. Also, you don't really need to do the multiple gliderecords you could just dot-walk to the user fields you want in the original gr variable while you loop through the records. I didn't make that change for you, but you could do it that way if you want.
(function runMailScript(current, template, email, email_action, event) {
var arr = [];
var gr = new GlideRecord('sysapproval_approver');
gr.addQuery('document_id',current.sys_id);
gr.addQuery('state','requested');
gr.query();
while(gr.next()) {
arr.push(gr.approver.toString());
}
var rows = '';
for(var i=0;i<arr.length;i++) {
var usr = new GlideRecord('sys_user');
usr.addQuery('sys_id',arr[i]);
usr.query();
if(usr.next()) {
rows += '<tr>';
rows += '<td>' + usr.first_name + ' ' + usr.last_name + '</td>';
rows += '<td>' + usr.title + '</td>';
rows += '<td>' + usr.email + '</td>';
rows +='</tr>';
}
}
var data;
data = '<table style="border-collapse: collapse;" border="1" cellpadding="5"><thead>';
data += '<tr><th>Name</th><th>Position</th><th>Email</th></tr>';
data += '</thead><tbody>';
data += rows;
data += '</tbody></table>';
template.print(data);
})(current, template, email, email_action, event);
If you want to do it in one GR instead of multiples, you can change it like this:
(function runMailScript(current, template, email, email_action, event) {
var rows = '';
var gr = new GlideRecord('sysapproval_approver');
gr.addQuery('document_id', current.sys_id);
gr.addQuery('state', 'requested');
gr.query();
while(gr.next()) {
rows += '<tr>';
rows += '<td>' + gr.approver.first_name + ' ' + gr.approver.last_name + '</td>';
rows += '<td>' + gr.approver.title + '</td>';
rows += '<td>' + gr.approver.email + '</td>';
rows +='</tr>';
}
var data;
data = '<table style="border-collapse: collapse;" border="1" cellpadding="5"><thead>';
data += '<tr><th>Name</th><th>Position</th><th>Email</th></tr>';
data += '</thead><tbody>';
data += rows;
data += '</tbody></table>';
template.print(data);
})(current, template, email, email_action, event);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-02-2018 12:12 PM
Try this:
for(var i=0;i<arr.length-1;i++)
Log the Length and see if this matches to the approver number.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-02-2018 01:15 PM
It returned 4 and there are 4 approvers.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-02-2018 12:17 PM
Looks like you're mixing single and double quotes. Try using single here. Not sure if this will resolve your issue.
rows += '<td>' + usr.first_name + " " + usr.last_name + '</td>';

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-02-2018 01:23 PM
Couple of issues in your script:
1. set rows to an empty string initially. otherwise, it may show undefined like you are seeing.
2. your html is not correct in the "data" string. see my changes below to that as well. You don't have to use the "thead" part I injected, but you need the additional "tr" I put in there to wrap the "th" cells.
this is untested, but should get you what you need. Also, you don't really need to do the multiple gliderecords you could just dot-walk to the user fields you want in the original gr variable while you loop through the records. I didn't make that change for you, but you could do it that way if you want.
(function runMailScript(current, template, email, email_action, event) {
var arr = [];
var gr = new GlideRecord('sysapproval_approver');
gr.addQuery('document_id',current.sys_id);
gr.addQuery('state','requested');
gr.query();
while(gr.next()) {
arr.push(gr.approver.toString());
}
var rows = '';
for(var i=0;i<arr.length;i++) {
var usr = new GlideRecord('sys_user');
usr.addQuery('sys_id',arr[i]);
usr.query();
if(usr.next()) {
rows += '<tr>';
rows += '<td>' + usr.first_name + ' ' + usr.last_name + '</td>';
rows += '<td>' + usr.title + '</td>';
rows += '<td>' + usr.email + '</td>';
rows +='</tr>';
}
}
var data;
data = '<table style="border-collapse: collapse;" border="1" cellpadding="5"><thead>';
data += '<tr><th>Name</th><th>Position</th><th>Email</th></tr>';
data += '</thead><tbody>';
data += rows;
data += '</tbody></table>';
template.print(data);
})(current, template, email, email_action, event);
If you want to do it in one GR instead of multiples, you can change it like this:
(function runMailScript(current, template, email, email_action, event) {
var rows = '';
var gr = new GlideRecord('sysapproval_approver');
gr.addQuery('document_id', current.sys_id);
gr.addQuery('state', 'requested');
gr.query();
while(gr.next()) {
rows += '<tr>';
rows += '<td>' + gr.approver.first_name + ' ' + gr.approver.last_name + '</td>';
rows += '<td>' + gr.approver.title + '</td>';
rows += '<td>' + gr.approver.email + '</td>';
rows +='</tr>';
}
var data;
data = '<table style="border-collapse: collapse;" border="1" cellpadding="5"><thead>';
data += '<tr><th>Name</th><th>Position</th><th>Email</th></tr>';
data += '</thead><tbody>';
data += rows;
data += '</tbody></table>';
template.print(data);
})(current, template, email, email_action, event);