How can I hide "selected variables" on e-mail notification if left empty?

Brent Cox
Mega Guru

We have an e-mail notification that goes out to users when their ticket is created. There is a mail script that is used to list all the variables in the ticket. How can I modify this script to ONLY display variables that are not empty? We are doing some mapping and it listing empty variables is causing issues. Here is the current script being used: 

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */ event) {
    
    var gr = new GlideRecord('question_answer');
    gr.addQuery('table_name''incident');
    gr.addQuery('table_sys_id', current.sys_id.toString());
    gr.query();
    
    if(gr.hasNext()){
        
        template.print("Selected variables:<br />");
        
        while(gr.next()){
            
            var qId = gr.question.toString();
            
            var answer = gr.value;
            
            // check if question has a reference
            if(gr.question.reference){
                
                var grRef = new GlideRecord(gr.question.reference);
                if(grRef.get(answer)){
                    answer = grRef.getDisplayValue();
                }
                
            }
            
            // check if answer needs to be looked up in different table
            if(gr.question.lookup_table){
                
                var grQlookup = new GlideRecord(gr.question.lookup_table);
                grQlookup.addQuery(gr.question.lookup_value, answer);
                grQlookup.query();
                
                if(grQlookup.next()){
                    answer = grQlookup.getDisplayValue();
                }
                
            }
            
            // check if answer needs to be looked up from choices
            var grQuestion = new GlideRecord('question_choice');
            grQuestion.addQuery('question', qId);
            grQuestion.addQuery('value', answer);
            grQuestion.query();
            
            if(grQuestion.next()){
                answer = grQuestion.text;
            }
            
            template.print(gr.question.getDisplayValue() + ' = ' + answer + '<br />');
            
        }
        
    }
    
})(current, template, email, email_action, event);
1 ACCEPTED SOLUTION

Harsh Vardhan
Giga Patron

Hi @Brent Cox  : Have you tried using below script ?

 

 

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

	// Add your code here

	template.print('Variable Summary: <br/>');
	
	var variables = current.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 != '' && value != ''){
			template.space(4);
			template.print('  ' + label + " = " + value + "<br/>");
		}
	} 

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

View solution in original post

2 REPLIES 2

Harsh Vardhan
Giga Patron

Hi @Brent Cox  : Have you tried using below script ?

 

 

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

	// Add your code here

	template.print('Variable Summary: <br/>');
	
	var variables = current.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 != '' && value != ''){
			template.space(4);
			template.print('  ' + label + " = " + value + "<br/>");
		}
	} 

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

Brent Cox
Mega Guru

I think this is exactly what I am needing. Thank you so much!