Approval Email Display RITM Ticket Variables - Following Catalog Item UI Policies

josgarcia
Tera Expert

Hello, I followed this ticket here, Adding RITM Variables in Approval Mail Notification, which resolved the issue with not getting variables to be displayed on approval requests. However, the there is a new issue with this fix. The email variables that are displayed are not following the UI catalog policies to display what the user had submitted within the ESC.

Can someone help please adjust the script so that it can display exactly what the user had submitted following the catalog UI policies that make the item show proper information that is displayed within the ESC?

 

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

	// Add your code here

	template.print('Summary of requested item: <br/>');

	var ritm = new GlideRecord('sc_req_item');
	ritm.addQuery('sys_id', current.sysapproval);
	ritm.query();
	if(ritm.next()){
		var variables = ritm.variables.getElements(); 
		for (var i=0;i<variables.length;i++) { 
			var question = variables[i].getQuestion();
			var label = question.getLabel();
			var value = question.getDisplayValue();
			if(label != ''){
				template.space(4);
				template.print('  ' + label + " = " + value + "<br/>");
			}
		} 
	}

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


I understand that the following response on that same ticket has a script that will only display fields that have a value, but this isn't always helpful as some Rich Text, check boxes, or even single/ multi-line text variables could have pre-filled values from the catalog item. The objective is to ensure the approver can see only what the user had submitted so that they know what they need to approve.

 

Thank you

2 ACCEPTED SOLUTIONS

Here is the full code I use. It allows you to display it on multiple notification based on the table that it is being triggered from.

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {
	var item_sys_id;
	var tableName = current.getTableName();		
	if(tableName == 'sysapproval_approver'){
		item_sys_id = current.sysapproval;		
	} else if (tableName == 'sc_task'){
		item_sys_id = current.request_item.sys_id;
	} else if(tableName == 'sc_req_item'){
		item_sys_id = current.sys_id; 
	} else{
		item_sys_id ='';
	}
	//gs.log("SYS ID : "+item_sys_id);
    var item = new GlideRecord("sc_req_item");
    item.addQuery("sys_id", item_sys_id);
    item.query();
    while (item.next()) {
        template.print("<div style='font-size:12pt;font-family:Arial, Helvetica, sans-serif;'><span><b>Options</b>:</span><br />");
        var keys = [];
        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() != '') { //This displays all of the variables (answered/unanswered)
                      
                var val = vs.get(i).getDisplayValue();
                if (val != '') {
					template.space(4);
                    template.print(vs.get(i).getLabel() + " : " + val + "<br />");
                }
              }
        }
        template.print("</div>");
    }
    // Add your code here
})(current, template, email, email_action, event);

View solution in original post

This should go thought all variables no matter if they are in a variable set and variables directly on a catalog item. They only things this does not work for is multi-row variable sets.

View solution in original post

7 REPLIES 7

Hey Brian, I know that I marked this as resolved, but I am recognizing something here.

 

 

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

    var item_sys_id;
    var tableName = current.getTableName();        
    if(tableName == 'sysapproval_approver'){
        item_sys_id = current.sysapproval;        
    } else if (tableName == 'sc_task'){
        item_sys_id = current.request_item.sys_id;
    } else if(tableName == 'sc_req_item'){
        item_sys_id = current.sys_id; 
    } else{
        item_sys_id ='';
    }

    var item = new GlideRecord("sc_req_item");
    item.addQuery("sys_id", item_sys_id);
    item.query();

    while (item.next()) {
        // Define table with borders for rows and columns
        template.print("<table style='width: 100%; border-collapse: collapse; font-family: Arial, sans-serif; font-size: 12pt;'>");
        template.print("<tr><th style='border: 1px solid #ddd; text-align: left; padding: 8px;'>Question</th><th style='border: 1px solid #ddd; text-align: left; padding: 8px;'>Answer</th></tr>");

        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() != '') {
                var label = vs.get(i).getLabel();
                var val = vs.get(i).getDisplayValue();

                if (val != '') {
                    template.print("<tr><td style='border: 1px solid #ddd; padding: 8px;'>" + label + "</td><td style='border: 1px solid #ddd; padding: 8px;'>" + val + "</td></tr>");
                }
            }
        }

        template.print("</table>");
    }

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

 

 

This script is one I had adjusted based off of what you gave me. However, I am seeing that it only applies filtering for catalog items with Variable Sets and not for Static Variables.

Is it possible to filter for those that have variables sets, no variable sets, or has both variable sets and static variables to ensure that it filters properly for all items?

This should go thought all variables no matter if they are in a variable set and variables directly on a catalog item. They only things this does not work for is multi-row variable sets.

Ohhhhh I found the issue. It wasn't the code. If it's supposed to filter out everything that doesn't have a value, and the Select Box doesn't have include '-- None --', as it's default option, it'll act as if the select box provided a value so it'll show on the approval request.

 

I got it taken care of now. Thanks Brian! I appreciate you letting me know that.