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

Brian Lancaster
Tera Sage

After your if (label != "") user the following code instead of what you have. This will make it print only variables that were filled out.

var val = vs.get(i).getDisplayValue();
if (val != '') {
	template.space(4);
        template.print(vs.get(i).getLabel() + " : " + val + "<br />");
}

That did not resolve the issue unfortunately and made it so that nothing is displayed now.

Screenshot 2024-09-26 at 10.25.59 AM.png

 Here is the code I adjusted it to following your recommendation: 

 

(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 val = vs.get(i).getDisplayValue();
            if (val != '') {
                template.space(4);
                template.print(vs.get(i).getLabel() + " : " + val + "<br />");
            }
        }
    }

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

Oh. I see. When you say vs.get you were referring to the var variables = ritm.variables.getElements();.

I adjusted the script to the following: 

(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 val = variables.get(i).getDisplayValue();
            if (val != '') {
                template.space(4);
                template.print(variables.get(i).getLabel() + " : " + val + "<br />");
            }
        }
    }

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


Here are the results of that.

 

Screenshot 2024-09-26 at 10.38.19 AM.png

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);