Jump to content

If then else is null rule


Recommended Posts

I need help figuring out where I'm going wrong on this script. I used the Text Rule Wizard. I want to print a block of formated text (resource) whenever a field is null. "is null" is not one of my choices in the Text Rule Wizard. I improvised & typed it into the wizard. Here's what it looks like:

If Text value of [field] is null

Then Return [resource], [field]

When I use the rule - nothing happens. Here's the javascript looks like:

if (Field("EMAIL") == String("null"))

{

return "<span>" + Resource("EmailMsg").content + Field("EMAIL") + "</span>";

}

return "";

I can do this kind of stuff in Access, but for some reason, FP confuses me.

I'm hoping someone besides me works weekends because I need to figure this out over the weekend for production on Monday.

Thanks,

Shelly

 

P.S. I also need to do a similar script for inserting a comma between two fields if one of the fields "is not null".

Link to comment
Share on other sites

You most likely just want to compare the Field value to an empty string, like so:

if (Field("EMAIL") == "")
 return "";
// else do something else with the non-empty value...

String("null") simply refers to a literal string whose value is the word "null". JavaScript does have the concept of a variable (object) with a null value, but it's seldom used, and FusionPro's built-in functions (Field, Rule, Resource, etc.) never return a null value. FusionPro doesn't have quite the same concept of a "null" field as in an SQL query to a database application such as Access.

 

If the named data field exists in the input data source for the job, then the Field function always returns a valid string (which may be empty). If the field does not exist, then the Field function throws an exception, which can be handled with a try/catch block.

 

So, if you have a field in the job, and you want to test if the value is empty, you should simply compare it to an empty string, as above. Or you can take advantage of JavaScript's built-in conversion to Boolean where an empty string is treated as false by using the "not" operator, like so:

if (!Field("EMAIL"))
 return "";

Again, the code above detects the case where the field is valid and present in the data source, but with an empty value for a particular record. This is different than the case where the field is undefined in the job; in that case, an exception is thrown, which will report an error in your log file, and (for text variables) will result in the field name being placed in curly brackets in the output. If you want to detect this particular case where the field is undefined, you can use a try/catch block like so:

try
{
 if (!Field("EMAIL"))
 return "";
}
catch (e)
{
 // do something else here?
 Print("Field is undefined!");
 return "";
}

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...