How to shows multiple variables of a related RITM in a task notification

Jacob64
Kilo Guru

Hi all,

We have a agreement with a vendor that send out emails when we have a request for them.
And this email is a notification of a sctask that is part of a RITM. This is easy to configure and is running currently.
However in this email notification they want to see the variables we use in the related RITM.

So what had in mind is using a general mail_script that we can use for all tasks of different requests with different variables (all catalog item depended). the scrip should ask for the related RITM number and then get all the availabe variables for that RITM and showing the name of the variable and its value.

Is this possible and if so how should it look like?

Many thanks in advance.

1 ACCEPTED SOLUTION

Hi Hayo,

In the meantime I found another mail script and slightly adjusted it to make it work for me:

 

(function runMailScript(current, template, email, email_action, event) {

var item = new GlideRecord("sc_req_item");
item.addQuery("sys_id", current.request_item);
item.query();
while (item.next()) {

template.print("<hr style='width: 98%;' /><p><b><u>Task Details</u></b></p>");

var keys = new Array();
var set = new GlideappVariablePoolQuestionSet();

set.setRequestID(item.sys_id);
set.setTaskID(current.sys_id);
set.load();

var vs = set.getFlatQuestions();
for (var i = 0; i < vs.size(); i++) {
if (vs.getLabel() != "" && vs.getDisplayValue() != "" && vs.getDisplayValue() != 'false' && vs.get(i).getDisplayValue() != 'false' && vs.get(i).getDisplayValue() != '') {
template.print("<p><b>" + vs.get(i).getLabel() + "</b>: " + vs.get(i).getDisplayValue() + "</p>");

}
}
}


})(current, template, email, email_action, event);

I hope it works for you as well.

Regards
Jacob.

View solution in original post

4 REPLIES 4

Hayo Lubbers
Kilo Sage

Hi @Jacob64 ,

 

Have a look at the out-of-the-box mail_script "request_item_approval" : https://<your instance>.service-now.com/nav_to.do?uri=sys_script_email.do?sys_id=b5eb7ce8b4fe7050f87755e926c3862a

 

You will see there is a default script include available for the request notifications.

 

Just printing the variables would look something like this:

 

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

    //Current is probably your sctask, so you want to have the Request Item (sys_id)
    var requestItemId = current.getValue('request_item'); //The sys_id of the Requested Item from your task
    var requestedFor = current.request.requested_for; //Not necessary for your use case

    var notificationUtil = new global.RequestNotificationUtil();
    var requestItemDetails = notificationUtil.getRequestItemDetails(requestItemId, requestedFor, true);

    if (requestItemDetails.variables.length > 0) {
		//This function returns the HTML directly as 'template.print'
        notificationUtil.setRequestItemVariablestoTemplate(requestItemDetails, template);
    }

});

 

 

Hope this helps. If it does, please thumbs up and/or accept the solution.

Hayo

Jacob64
Kilo Guru

Hi Hayo,

I've been trying this but without any result.

I have created a New Script based on the script you gave me and called it "request_item_variables"

Then in the Email notification I added the following:
Variables: ${mail_script:request_item_variables}
This gives me nothing. Just an empty line after the word "Variables:"
Apperently I do something wrong here.

Kind Regards

Jacob

Hi @Jacob64 ,

 

Can you share the mailscript? Maybe I can see what is going wrong.

 

And the name of the script is case sensitive, so please make sure that is also correct. You can test that by adding a template.print('hello world');

 

Document on the mail script : https://www.servicenow.com/docs/bundle/xanadu-platform-administration/page/script/server-scripting/c...

 

Regards,

Hayo

Hi Hayo,

In the meantime I found another mail script and slightly adjusted it to make it work for me:

 

(function runMailScript(current, template, email, email_action, event) {

var item = new GlideRecord("sc_req_item");
item.addQuery("sys_id", current.request_item);
item.query();
while (item.next()) {

template.print("<hr style='width: 98%;' /><p><b><u>Task Details</u></b></p>");

var keys = new Array();
var set = new GlideappVariablePoolQuestionSet();

set.setRequestID(item.sys_id);
set.setTaskID(current.sys_id);
set.load();

var vs = set.getFlatQuestions();
for (var i = 0; i < vs.size(); i++) {
if (vs.getLabel() != "" && vs.getDisplayValue() != "" && vs.getDisplayValue() != 'false' && vs.get(i).getDisplayValue() != 'false' && vs.get(i).getDisplayValue() != '') {
template.print("<p><b>" + vs.get(i).getLabel() + "</b>: " + vs.get(i).getDisplayValue() + "</p>");

}
}
}


})(current, template, email, email_action, event);

I hope it works for you as well.

Regards
Jacob.