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

Community Alums
Not applicable

Hi @Koyel Guha 

 

Please check the below code. Tested in PDI & working as expected.

If the variable is inactive the below code ignores the value.

 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 (var key in gr.variables) {
        var v = gr.variables[key];
        var question = v.getGlideObject().getQuestion();
        if (question.active && question.getLabel() != '') { //checking active or not
            var displayValue = v.getDisplayValue();
            if (displayValue) {
                template.space(4);
                template.print('         ' + question.getLabel() + " = " + displayValue + "\n");
            }
        }
    }
}

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

Hi Sai,

Thank you for your response. 

Could you please explain about what you mentioned regarding variable is inactive ?

I want these scenarios below : 

 

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

Thanks in advance.

Community Alums
Not applicable

The code which I provided checks if the catalog variable is active or inactive. I guess this is not your requirement.

Now that you have explained your requirement let me check. 

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.