Hide field on email notification if that field is empty in catalog item.

abhaysingh98
Tera Contributor

Hi guys,

 

I have a requirement where catalog items have a price field, when this fields is filled in catalog item, then I would like to add this field to the approval email else I want this field to be hidden in approval mail.

I have created an email script to show this price field on email notification to the approver but I am not sure how to hide this field or cell value in email notification if price field is empty in catalog item.

 

 

7 REPLIES 7

Mark Manders
Mega Patron

Can you share your email script as well? That helps in explaining what change is needed. It will probably be something like

var itemPrice;
var price = current.catalog_item.price;
if(price != 0){
    itemPrice = '\nPrice: $' + price;
} else {
    itemPrice = '';
}
var emailText = 'Dear approver,\n\nPlease approve this item\nITEM: ' + catalog_item.name + itemPrice + '\nRequested by: ' + and_so_on_and_so_on;

Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Hi @Mark Manders,

This Email notification is on sys_approval table. Below is the email script that I used.

 

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
          /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
          /* Optional GlideRecord */ event) {
    if(current.sysapproval.recurring_price != ''){
    template.print(current.sysapproval.recurring_price.getReferenceCurrencyCode() + "" + parseFloat(current.sysapproval.quantity * current.sysapproval.recurring_price.getReferenceValue()).toFixed(2));
    }
   
})(current, template, email, email_action, event);

The price field is always filled, so it's never empty. Try something like

 

 

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
          /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
          /* Optional GlideRecord */ event) {
    if(current.sysapproval.recurring_price != '0'){
    template.print(current.sysapproval.recurring_price.getReferenceCurrencyCode() + "" + parseFloat(current.sysapproval.quantity * current.sysapproval.recurring_price.getReferenceValue()).toFixed(2));
    }
   
})(current, template, email, email_action, event);

 

 

By extending the line with template.print(), you could add even more information that is only relevant when there is a price value. Like

 

 

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
          /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
          /* Optional GlideRecord */ event) {
    if(current.sysapproval.recurring_price != ''){
    template.print("We expect this request to cost" + current.sysapproval.recurring_price.getReferenceCurrencyCode() + "" + parseFloat(current.sysapproval.quantity * current.sysapproval.recurring_price.getReferenceValue()).toFixed(2));
    }
   
})(current, template, email, email_action, event);

 

Rajesh Chopade1
Mega Sage

Hi @abhaysingh98 

For normal fields we can use hasField() method to confirm if the field exist but it doesn't work for variables so what you can do is, in the email script try something like below

 

 

if (current.variables.var_name) {
        // The variable exists and is not null
} else {
        // The variable does not exist
}

 

 

thank you

rajesh