How to ignore the false variable value from the email script notification

Koyel Guha
Tera Contributor

Hi All,

 

We have an existing script to show the RITM variables in the notification. However, its also showing the variables with false values. Can you suggest how to hide the false variable value from the below script.

 

template.print("<p></p>Requested items:\n");
var gr = new GlideRecord("sc_req_item");
gr.addQuery("request", current.sys_id);
gr.query();
while (gr.next()) {
    var stage = gr.stage.getDisplayValue();
    if (JSUtil.nil(stage))
        stage = gr.stage.getChoiceValue();
    template.print(gr.number + ":  " + gr.cat_item.getDisplayValue() + ", Stage: " + stage + "\n");
    template.print(" \n ");
    template.print("       More details:\n");
    for (key in gr.variables) {
        var v = gr.variables[key];
        if (v.getGlideObject().getQuestion().getLabel() != '') {
            if (v.getDisplayValue() != '') {
                template.space(4);
                template.print('         ' + v.getGlideObject().getQuestion().getLabel() + " = " + v.getDisplayValue() + "\n");
            }
        }
    }
}
 
Thanks in advance.
1 ACCEPTED SOLUTION

Community Alums
Not applicable

Hi @Koyel Guha ,

 

Check the below code:

template.print("<p></p>Requested items:\n");
var gr = new GlideRecord("sc_req_item");
gr.addQuery("request", current.sys_id);
gr.query();
while (gr.next()) {
    var stage = gr.stage.getDisplayValue();
    if (JSUtil.nil(stage))
        stage = gr.stage.getChoiceValue();
    template.print(gr.number + ":  " + gr.cat_item.getDisplayValue() + ", Stage: " + stage + "\n");
    template.print(" \n ");
    template.print("       More details:\n");
    for (key in gr.variables) {
        var v = gr.variables[key];
        var question = v.getGlideObject().getQuestion();
        var label = question.getLabel();
        var displayValue = v.getDisplayValue();
        if (label && displayValue && displayValue.toLowerCase() !== 'false') {
            template.space(4);
            template.print('         ' + label + " = " + displayValue + "\n");
        }
    }
}

 

Result:

1. here variable "advance" is empty. In email body the above script didn't print "advance"

Sai149_0-1716898956984.pngSai149_1-1716898966522.png

 

2. here variable "tt" is not checked & select box "Bu 2" is false & "advance" is empty. In email body the above script didn't print "advance"

Sai149_2-1716899138209.png

Sai149_3-1716899154019.png

If my answer helped you in any way, please mark it as helpful or correct. 

 

View solution in original post

7 REPLIES 7

James Chun
Kilo Patron

Hi @Koyel Guha,

 

Can you elaborate on what you mean by false values?

My guess is that it should cover the following scenarios:

  • No value is provided for the variable
  • A checkbox-type variable is not selected
  • Select box-type variable where false selected

Cheers

Hi James,

You have mentioned the correct scenarios above.

 

 

palanikumar
Mega Sage

Hi @Koyel Guha ,

Please try this code. I have highlighted the changes suggested in bold:

template.print("<p></p>Requested items:\n");
var gr = new GlideRecord("sc_req_item");
gr.addQuery("request", current.sys_id);
gr.query();
while (gr.next()) {
    var stage = gr.stage.getDisplayValue();
    if (JSUtil.nil(stage))
        stage = gr.stage.getChoiceValue();
    template.print(gr.number + ":  " + gr.cat_item.getDisplayValue() + ", Stage: " + stage + "\n");
    template.print(" \n ");
    template.print("       More details:\n");
    for (key in gr.variables) {
        var v = gr.variables[key];
        if (v.getGlideObject().getQuestion().getLabel() != '') {
            if (v.getDisplayValue() != '' && v.getDisplayValue() != 'false') {
                template.space(4);
                template.print('         ' + v.getGlideObject().getQuestion().getLabel() + " = " + v.getDisplayValue() + "\n");
            }
        }
    }
}
Thank you,
Palani