How to check if a RITM variable has value in approval notification email script?

subhadeep1618
Tera Guru

I want to print a variable value in approval notification script, only when the variable exists in the corresponding RITM.

The name of the catalog variable is city, the variable type is single-line text (i.e. String).

Below is my code:

var v_city = "${sysapproval.variables.city}";
if(v_city)
	template.print("City: ${sysapproval.variables.city}");

If user has entered 'New York' in the catalog field, the above prints: City: New York --- which is perfectly ok.

But if the city variable was hidden (not displayed through some UI policy) in the catalog, then the above prints:

City:   --- blank value is shown here

What I really want is not to print anything when that variable was hidden in the catalog from the user.

Can somebody please tell what I am doing wrong here.

 

Thanks.


Please mark this post as a solution and also as helpful, if this resolves your issue or query.

Thanks,
Subhadeep Ghosh.
1 ACCEPTED SOLUTION

Hi @subhadeep1618 

 

Checked by putting logs, if variable value is empty then gs.log(${sysapproval.variables.city}) returning some value, not sure what but I think it may be # code or something.

 

Below code worked for me...!!

 

var v_city = current.sysapproval.variables.city;
if(v_city == '') {
	template.print("");
} else {
       template.print("City: ${sysapproval.variables.city}");
}

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

View solution in original post

10 REPLIES 10

Hi @subhadeep1618 

 

Checked by putting logs, if variable value is empty then gs.log(${sysapproval.variables.city}) returning some value, not sure what but I think it may be # code or something.

 

Below code worked for me...!!

 

var v_city = current.sysapproval.variables.city;
if(v_city == '') {
	template.print("");
} else {
       template.print("City: ${sysapproval.variables.city}");
}

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

Hi @Vishal Birajdar ,

Lots of thanks for taking your time and helping me out here.

Your solution has finally worked. 

This was so simple, yet so hard to find.

Hope we interact more in the future.

 

Thanks,

Subhadeep.


Please mark this post as a solution and also as helpful, if this resolves your issue or query.

Thanks,
Subhadeep Ghosh.

Hi @subhadeep1618 

 

Definitely...!!! we can...!!

Happy to help..!

 

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

Harish KM
Kilo Patron
Kilo Patron

Hi @subhadeep1618 You can create a email script and call the email script in Notification.

Email Script:

if(current.sysapproval.variables.city){
template.print('city:' + current.sysapproval.variables.city+ '<br/>');
}

Regards
Harish

Anand Kumar P
Giga Patron
Giga Patron

Hi @subhadeep1618 ,
Convert into string and trim whitespaces if any use below script.

if (current.sysapproval.variables.city) {
 var cityValue = current.sysapproval.variables.city.toString().trim(); 
if (cityValue !== '') { 
template.print("City: " + cityValue); 
} 
}

Thanks,

Anand