mail script not working in workflow notification

chrisn_
Mega Guru

Hello,

I have been using a mail script in an approval email that sucks up all of the variables on an RITM and spits them out in an email. This has been working in the approval email, but now I am trying to use it in a workflow notification and it is failing. I was hoping someone might be able to provide some insight as to why. Here is the script I am using.

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */ event) {

// Add your code here
var item = new GlideRecord("sc_req_item");
item.addQuery("sys_id",current.sc_req_item );
item.query();
while(item.next()) {
var fields = item.getFields(); //gets the name and values of all the fields the query returns
//gs.log(item.getAttribute('name') + " EPO approval mail script");
gs.log('Enumerating over all fields with values:');
for (var m = 0; m < fields.size(); m++) {
var glideElement = fields.get(m);
}

gs.print('');
var keys = new Array();
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() != '' && vs.get(i).getValue()!='') {
if(vs.get(i).getLabel() != '' && vs.get(i).getDisplayValue()!='' && vs.get(i).getDisplayValue().toString() != 'false')
{
template.space(4);
template.print(' ' + vs.get(i).getLabel() + " = " + vs.get(i).getDisplayValue() + "\n");
}
}
}
}
})(current, template, email, email_action, event);

7 REPLIES 7

Maverick E
Tera Guru

Hello Chris,

You could just simply create another notification with the same email script and make your changes to it. Create a custom event for this notification. Then in the workflow just trigger the event you made that is attached to the new notification. 

Hello Maverick,

Currently this script and it's function is not something we really want a lot of people to be using as its just more email. The original version of this only runs for 1 specific item approval. I was hoping to keep it this way by embedding it just in the body of a workflow notification.

I think I am not referencing the table properly?

I would still recommend creating another notification and adding conditions to make it only run if the item matches. I am not sure if you can even have a script in a notification within a workflow. 

Here is a link to a post I found that may give you more insight to my response:
https://community.servicenow.com/community?id=community_question&sys_id=fbc34ba5dbd8dbc01dcaf3231f96...