All Catalog Task variables displayed in a Notification

Wayne Richmond
Tera Guru

Hi all. I recently found this excellent Email Script that displays all Requested Item variables in a Notification if they are not empty:

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

    // Get Requested Item
    var reqitem = current.request_item;

    // Get Owned Variables for Requested Item and sort by Order
    var ownvar = new GlideRecord('sc_item_option_mtom');
    ownvar.addQuery('request_item.number', reqitem.number);
    ownvar.addQuery('sc_item_option.value', '!=', '');
    ownvar.orderBy('sc_item_option.order');
    ownvar.query();
    while (ownvar.next()) {

        // Add Question, Answer and Order into notification mail
        // Set variable v to variable name
        var field = ownvar.sc_item_option.item_option_new;
        var fieldValue = ownvar.sc_item_option.item_option_new.name;

        // Print variable name and Display Value for each variable in Requested Item
        template.print('<p><b>' + field.getDisplayValue() + '</b>' + ': ' + reqitem.variables[fieldValue].getDisplayValue()) + '</p>';

    }

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

However, my requirement is to achieve the same thing, but only have the Notification display the variables on a Catalog Task. As you know, from the Workflow Editor, you can specify which variables from a RITM you want to display in a Catalog Task - these are the ones I want to extract.

Any thoughts?

1 ACCEPTED SOLUTION

Hi @Wayne Richmond 

Just change your script to this

(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()) {
        
 var keys = new Array();
        var set = new GlideappVariablePoolQuestionSet();

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

        var vs = set.getFlatQuestions();
if(vs.size() == '0' || vs.size() == '')
{
return;
}
else
{
		template.print("<hr style='width: 98%;' /><p><b><u>Task Details</u></b></p>");
		
          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);

Sorry script not tested but try it once 🙂

Thank you
Prasad

View solution in original post

5 REPLIES 5

Hi Prasad.

I was looking for the same and found your last script very good. It works like a charm!

Many thanks!!

Kind regards,
Jacob.