Email Mail Script to only display Variables with an answer from a Record Producer

jonathangilbert
Kilo Sage

Morning all

 

I have seen there are lots of posts regarding the displaying of Variables in an email notification. I have come across this one:- 

 

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

    // Add your code here

    template.print('Full Details:- <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 != ''){
            template.space(4);
            template.print('  ' + label + " = " + value + "<br/>");
        }
    }

})(current, template, email, email_action, event);
 
But it shows every single Variable for the catalog Item. How do you get it only to display the variables that have a value against them.
As you can appreciate Cat items have UI policies to display/hide variables depending on certain scenerios, so I kind of want this on the variables displayed, as it is pointless displaying a variable in the notification if it wasnt displayed on the catalog item
I also want to include Multirow variables in this too if possible
1 ACCEPTED SOLUTION

Morning Avocado,

 

Thank you for the reply. I actually figured it out last night and it is simply as easy as changing 

 

 if(label != ''){

 

to this 

 

if(value!= ''&&  value!= 'false'){

 

Another example of overthinking things!. It works perfectly now. Here is the full script, with improved formatting if anyone else wishes to use it :-

 

// Add your code here

template.print('<b>Full Details:-</b> <br/><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(value!= ''&& value!= 'false'){
template.space(4);
template.print('<b> ' + label + '</b>' + " = " + value + "<br/><br/>");
}
}

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

    

   

   

View solution in original post

3 REPLIES 3

avocado1000
Tera Contributor

Try adding another condition for your if statement: 

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

    // Add your code here

    template.print('Full Details:- <br/>');
   
    var variables = current.variables.getElements();
    var question, 
           value;
    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);



Morning Avocado,

 

Thank you for the reply. I actually figured it out last night and it is simply as easy as changing 

 

 if(label != ''){

 

to this 

 

if(value!= ''&&  value!= 'false'){

 

Another example of overthinking things!. It works perfectly now. Here is the full script, with improved formatting if anyone else wishes to use it :-

 

// Add your code here

template.print('<b>Full Details:-</b> <br/><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(value!= ''&& value!= 'false'){
template.space(4);
template.print('<b> ' + label + '</b>' + " = " + value + "<br/><br/>");
}
}

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

    

   

   

Could this be run on an email script for a notification on the sc_request table?