- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2022 10:58 AM
I'm currently building out a notification for when an assessment survey comes back with a subpar result.
I've been successful in triggering the notification via an event, but the e-mail that gets sent is blank.
My email scripted is named assessment.failed.body and I am calling it in the Notification as such:
${mail_script:assessment.failed.body}
Because I can't seem to dot-walk my way to get the data from the asmt_assessment_instance_question table that corresponds with the asmt_assessment_instance, I had to create a script to display the end-users responses for my management to be able to see pertinent information in the email notification.
Below is the script:
(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */ event) {
// Add your code here
var gr1 = new GlideRecord('asmt_assessment_instance_question');
var gr2 = new GlideRecord('asmt_assessment_instance_question');
var gr3 = new GlideRecord('asmt_assessment_instance_question');
var gr4 = new GlideRecord('asmt_assessment_instance_question');
var gr5 = new GlideRecord('asmt_assessment_instance_question');
var gr6 = new GlideRecord('asmt_assessment_instance_question');
var q1 = "How courteous and respectful was the technician who responded? (1=poor, 5=excellent)";
var q2 = "Please rate the technical competency of the technician serving you (1=poor, 5=excellent)";
var q3 = "How satisfied are you with your overall service experience? (1=not at all, 5=completely)";
var q4 = "How satisfied were you with the response time to your incident? (1=not at all, 5=completely)";
var q5 = "Was the IS Department able to resolve your issue?";
var q6 = "Any other comments you would like us to know?";
var query1 = 'instance=' + current.sys_ID + '^metric=142a2c44d7211100158ba6859e6103a4';
var query2 = 'instance=' + current.sys_ID + '^metric=542a2c44d7211100158ba6859e6103a4';
var query3 = 'instance=' + current.sys_ID + '^metric=d42a2c44d7211100158ba6859e6103a4';
var query4 = 'instance=' + current.sys_ID + '^metric=d02a2c44d7211100158ba6859e6103a4';
var query5 = 'instance=' + current.sys_ID + '^metric=942a2c44d7211100158ba6859e6103a4';
var query6 = 'instance=' + current.sys_ID + '^metric=902a2c44d7211100158ba6859e6103a4';
gr1.addEncodedQuery(query1);
gr1.query();
if(gr1.next())
{
var value1 = gr1.getValue(value);
template.print(q1);
template.print(value1);
}
gr2.addEncodedQuery(query2);
gr2.query();
if(gr2.next())
{
var value2 = gr2.getValue(value);
template.print(q2);
template.print(value2);
}
gr3.addEncodedQuery(query3);
gr3.query();
if(gr3.next())
{
var value3 = gr3.getValue(value);
template.print(q3);
template.print(value3);
}
gr4.addEncodedQuery(query4);
gr4.query();
if(gr4.next())
{
var value4 = gr4.getValue(value);
template.print(q4);
template.print(value4);
}
gr5.addEncodedQuery(query5);
gr5.query();
if(gr5.next())
{
var value5 = gr5.getValue(value);
template.print(q5);
template.print(value5);
}
gr6.addEncodedQuery(query6);
gr6.query();
if(gr6.next())
{
var value6 = gr6.getValue(string_value);
template.print(q6);
template.print(value6);
}
})(current, template, email, email_action, event);
//Query 1 is How courteous and respectful was the technician who responded? (1=poor, 5=excellent)
//Query 2 is Please rate the technical competency of the technician serving you (1=poor, 5=excellent)
//Query 3 is How satisfied are you with your overall service experience? (1=not at all, 5=completely)
//Query 4 is How satisfied were you with the response time to your incident? (1=not at all, 5=completely)
//Query 5 is Was the IS Department able to resolve your issue?
//Query 6 is Any other comments you would like us to know?
The resulting e-mail notification comes through blank and I can't determine where I went wrong. I am very green when it comes to scripting in js and using GlideRecords, so I could be so far off base.
Any feedback would be greatly appreciated.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2022 03:25 AM
Hi
Couple of issues in the script shared above which need to be corrected:
1) SYS ID should be written in lower case as sys_id which has been replaced from line number 23 to 28
2) Current Record ID you will not get by comparing the current Sys id with instance field and it need to be compared against the "source_id" field which is the correct field and stores the referenced record sys id.
I have modified that for you as well which is from line number 23 to 28 again
3) When you are suing getValue to fetch a value of a field, the field name should be in quotes which was not there.
4) At the end of function , I saw you have written 6 comments which was commented, but I have seen previously in one of my project couple of year back that writing a comment at the end of the function causes issue in trigger the script and was confirmed via HI ticket as well. So I have removed those comments, if you need to include comments may be you can include it in between the function some where say at line number 8 just before you are writing your glideRecord
I have updated the script for you, please use the modified script below:
(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */
email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */
event) {
// Add your code here
var gr1 = new GlideRecord('asmt_assessment_instance_question');
var gr2 = new GlideRecord('asmt_assessment_instance_question');
var gr3 = new GlideRecord('asmt_assessment_instance_question');
var gr4 = new GlideRecord('asmt_assessment_instance_question');
var gr5 = new GlideRecord('asmt_assessment_instance_question');
var gr6 = new GlideRecord('asmt_assessment_instance_question');
var q1 = "How courteous and respectful was the technician who responded? (1=poor, 5=excellent)";
var q2 = "Please rate the technical competency of the technician serving you (1=poor, 5=excellent)";
var q3 = "How satisfied are you with your overall service experience? (1=not at all, 5=completely)";
var q4 = "How satisfied were you with the response time to your incident? (1=not at all, 5=completely)";
var q5 = "Was the IS Department able to resolve your issue?";
var q6 = "Any other comments you would like us to know?";
var query1 = 'source_id=' + current.sys_id + '^metric=142a2c44d7211100158ba6859e6103a4';
var query2 = 'source_id=' + current.sys_id + '^metric=542a2c44d7211100158ba6859e6103a4';
var query3 = 'source_id=' + current.sys_id + '^metric=d42a2c44d7211100158ba6859e6103a4';
var query4 = 'source_id=' + current.sys_id + '^metric=d02a2c44d7211100158ba6859e6103a4';
var query5 = 'source_id=' + current.sys_id + '^metric=942a2c44d7211100158ba6859e6103a4';
var query6 = 'source_id=' + current.sys_id + '^metric=902a2c44d7211100158ba6859e6103a4';
gr1.addEncodedQuery(query1);
gr1.query();
if (gr1.next()) {
var value1 = gr1.getValue('value');
template.print(q1);
template.print(value1);
}
gr2.addEncodedQuery(query2);
gr2.query();
if (gr2.next()) {
var value2 = gr2.getValue('value');
template.print(q2);
template.print(value2);
}
gr3.addEncodedQuery(query3);
gr3.query();
if (gr3.next()) {
var value3 = gr3.getValue('value');
template.print(q3);
template.print(value3);
}
gr4.addEncodedQuery(query4);
gr4.query();
if (gr4.next()) {
var value4 = gr4.getValue('value');
template.print(q4);
template.print(value4);
}
gr5.addEncodedQuery(query5);
gr5.query();
if (gr5.next()) {
var value5 = gr5.getValue('value');
template.print(q5);
template.print(value5);
}
gr6.addEncodedQuery(query6);
gr6.query();
if (gr6.next()) {
var value6 = gr6.getValue('string_value');
template.print(q6);
template.print(value6);
}
})(current, template, email, email_action, event);
Also another suggestion here would be that you can make use of Functions which will improve the code performance and you do not need to write GlideRecord 6 times here. I was not sure of your exact instance details here on your requirement and all so have not modified it and just corrected the code which you want which was causing you the issue here.
Hope this helps. Please mark the answer as correct/helpful based on impact.
Regards,
Shloke
Regards,
Shloke
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2022 11:04 AM
A quick issue I found is the sys_id on query1 to query6 - it should be sys_id (all lowercase), you had sys_ID.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2022 11:11 AM
Thank you, I'm sure that would have caused issues if the script was firing correcting.
I corrected that typo but it's still triggering as a blank notification.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2022 11:29 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2022 11:30 AM
Your gr6 query is calling the undefined variable 'string_value' as well. That too will need to be defined. If they are the names of fields then you just need to wrap in quotes like so gr6.getValue('string_value').
Oh and template.print(gr1) won't display anything. Update to gr1.getDisplayValue().
Do the same for the others gr2 - gr6.