Field Formulas help - javascript - detecting empty field

Kevin_579059
Kevin_579059 Posts: 67
edited August 2020 in Questions

Hello,
I am trying to use the syntax at https://help.catalytic.com/docs/field-field-formulas/ for converting a field to True or False, specifically for the following example given:

Assume
fields['nullvalue'] is empty or null
then
result = !!fields['nullvalue'] Returns False

Here is my goal: If a field is NOT empty, I want to append some text to it. If the field is empty, then I want to ignore it and do nothing (and leave it empty).

So I have the following in a Field Formulas:

if (!!fields['field-testing-for-empty']) {
} else
{
result = 'appended text: ' + fields['field-testing-for-empty'];
}

This does not work as-is.

I do not want to just append the text regardless, because later in an Email action, I have a Javascript if {{#if field-testing-for-empty {{/if}} to insert text in the email if this field has any values. So if I just append the text in the first step, then it will have a unintended value in the Email step and appear in the email.

thanks!
Kevin

Answers

  • Chris_573086
    Chris_573086 Posts: 14 admin

    @Kevin_579059 I believe the boolean check you're doing is working as intended, but because your text append is in your else block, you're getting the opposite result. The following field formula results in the behavior you're after:

    result = ''
    
    if (!!fields['field-testing-for-empty']) {
        result = 'appended text: ' + fields['field-testing-for-empty'];
    }
    

    In your original formula, an empty string would initially be falsey, and the !! operators would convert it to TRUE and then back to FALSE. It then skips over the empty code block and enters the else block, which has your text append.

  • Thank you @Chris_401431 ! It worked!
    1. I left out result = ' ' (and it still works) What is that doing?
    2. Is there an easier way to test if a field is empty in javascript? (for use with field formulas)
    3. Is the !! 'changing' or 'converting' the boolean? In the example on the help docs, it assumes that fields['nullvalue'] is empty, so I would think that using the !! prior to it is giving a FALSE because the field is empty.
    thanks!
    Kevin

  • Chris_573086
    Chris_573086 Posts: 14 admin

    @Kevin_579059 I'm glad it's working now.
    1. The field formula expects some value to be set for result, and without an ELSE block, a blank string wouldn't set one. At least that's how my test behaved, but if both cases work for you without that there it should be fine to leave out.
    2. Javascript does a decent job checking if something is "truthy" when you plug it into a conditional. As long as an "empty" field resolves to an empty string, simply checking with if (fields['field-to-check']) should resolve to FALSE correctly.
    3. The "not" or "bang" operator ! switches a boolean value between TRUE and FALSE. When you use it in a conditional, it internally converts whatever variable you are checking into the opposite boolean. For example, the integer 1 would resolve as "truthy" in a conditional, but if (!variableEqualToOne) will resolve as FALSE. The double not operator !! converts a variable to the boolean that it resolves to internally, so a "truthy" string or integer becomes TRUE instead of the original value. You're right that !! in front of a "falsey" value resolves to FALSE. In this case your code just need to be in the IF block instead of the ELSE block.