- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-23-2018 05:09 PM
I have a notification email script that works great in the Global application. I'm unable to get it in my custom application though. The email script adds and displays all variables from a record producer within a sent email. Below are things I have tried and more details I discovered while troubleshooting:
- I created a copy of the working email script and put it in the custom application
- The record producer is in the custom application
- The record producer variable are in the custom application
- The email notifications are in the custom application
Any help would be appreciated.
Thanks.
Ty
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-26-2018 08:37 PM
As warning says, its scoped application in which getGlideObject can't be used, try reading in servicenow docs of alternative methods.
You are using method of GlideElement - Global methods in scope application
To use => GlideElement - Scoped methods
If you need to print variable name & its value.
You can simply print 'key' variable label & for value like
for (var key in current.variables) {
template.print('<p><font size="3" face="arial">');
template.print("<div><b>" + key + "</b>: " + current.variables[key] + "</div>\r\n");
}
Just in-case if all good & emails are not getting sent, you may need to just pass email id in this
email.setFrom("Facilities Help Desk <facilities.helpdesk@terumobct.com>");
email.setFrom("facilities.helpdesk@terumobct.com");
Mark this answer helpful, if it solves, if not let me know happy to help you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2018 04:04 PM
Thanks ratnesh! I really appreciate your input. It works almost perfectly.
Now the email script is displaying the variable names instead of the variable questions.
Any idea how to fix this?
Here is what the email script looks like now:
(function runMailScript(current, template, email, email_action, event) {
// Add your code here
email.setFrom("Facilities Help Desk <facilities.helpdesk@terumobct.com>");
for (var key in current.variables) {
template.print('<p><font size="3" face="arial">');
template.print("<div><b>" + key + "</b>: " + current.variables[key].getDisplayValue() + "</div>\r\n");
}
})(current, template, email, email_action, event);
Thanks!
Ty
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-10-2018 11:31 PM
I am glad to know it worked for you.
For your query, I tried to find something simple & straight, but could not. But able to do via some scripting.
Create a function which will accept two parameter sys_id of catalog item & variable name for which question needs to be find.
function getVariableName(catItem, variable_name){
var question='';
var gr=new GlideRecord('item_option_new');
gr.addQuery('cat_item',catItem);
gr.addQuery('name',variable_name);
gr.query();
if(gr.next()){
question=gr.question_text;
}
return question;
}
So your code go like this.
(function runMailScript(current, template, email, email_action, event) {
// Add your code here
email.setFrom("Facilities Help Desk <facilities.helpdesk@terumobct.com>");
for (var key in current.variables) {
template.print('<p><font size="3" face="arial">');
template.print("<div><b>" + getVariableName(current.cat_item, key) + "</b>: " + current.variables[key].getDisplayValue() + "</div>\r\n");
}
function getVariableName(catItem, variable_name) {
var question = '';
var gr = new GlideRecord('item_option_new');
gr.addQuery('cat_item', catItem);
gr.addQuery('name', variable_name);
gr.query();
if (gr.next()) {
question = gr.question_text;
}
return question;
}
})(current, template, email, email_action, event);
I believe making code modular so that it can be used from other scripts whenever needed. For this you can create a custom script include & call this via script include.
Sample you can see for JSUtil : which is out-of-box script include for common JS function to be used.
Do let me know if it worked or not worked for you