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

Danish Bhairag2
Tera Sage
Tera Sage

Hi @subhadeep1618 ,

 

You can just put the  condition like below. instead of only if(v_city) 

 

if(v_city != ''){

template.print("City: ${sysapproval.variables.city}")

}

 

Thanks,

Danish

 

Hi @Danish Bhairag2 ,

That's the very first thing I did, but it did not work - same issue I was getting.

Then I changed to if(v_city), but this is also not working.

 

Thanks.


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

Thanks,
Subhadeep Ghosh.

Vishal Birajdar
Giga Sage

Hi @subhadeep1618 

 

Can you try like this...!!! 

It's the same logic you have tried earlier...Just a little adjustment...

 

var v_city = "${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 ,

I've tried your suggestion, but now it is always going inside the else block.

So its the same issue.

Can you please check if there is a definite way to check NULL or Undefined value of variable? 

I am also checking.

 

Thanks.


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

Thanks,
Subhadeep Ghosh.