Need help with if, else syntax in Field:Field Formulas action

Options
Kevin_579059
Kevin_579059 Posts: 67
edited April 2020 in Questions

There are only two examples of using if and else on the help page for the the action "Field: Field Formulas" (https://help.catalytic.com/docs/field-field-formulas/). I am trying to write an if, else statement based on the value of a text field. However, no return field name is being created. I think it might have to do with my syntax (single equal sign versus double equal sign, single quote versus double quote, the semi-colon, etc.). Can someone take a look at the following:

if (fields[‘first-alpha-component‘] == 'C') { result = fields['design-numbers'].slice(0,9); } else if (fields[‘first-alpha-component‘] == 'F') { result = fields['design-numbers'].slice(0,8); } else if (fields[‘first-alpha-component‘] == 'W') { result = fields['design-numbers'].slice(0,8); } else { result = none; }

Note that if I remove all the if, else stuff and just put
result = fields['design-numbers'].slice(0,9)
it will work and the bot proceeds, so it is nothing wrong with the variable design-numbers

I know I can create multiple "Field Formulas" actions and use Conditions, but I would rather cut down on the number of actions/steps in the bot.
Thank you for your time!
Kevin

Tagged:

Best Answers

  • Dylan_191182
    Dylan_191182 Posts: 92 admin
    Answer ✓
    Options

    @Kevin_579059 I think the editor may have broken the formatting of your field formula so hard for me to tell exactly what wasn't right. My best guess is the issue was none wasn't enclosed in quotes.

    Here is a formatted version that should work:

    if (fields["first-alpha-component"] == "C") {
        result = fields["design-numbers"].slice(0, 9);
    } else if (fields["first-alpha-component"] == "F") {
        result = fields["design-numbers"].slice(0, 8);
    } else if (fields["first-alpha-component"] == "W") {
        result = fields["design-numbers"].slice(0, 8);
    } else {
        result = "none";
    }
    

    I've also attached a small sample Pushbot that shows a working version of the field formula you can test. Hope this helps!

  • Dylan_191182
    Dylan_191182 Posts: 92 admin
    Answer ✓
    Options

    Glad it worked!

    So does the formatting really matter?

    Not really. For example two spaces or one or a new line vs a space doesn't matter. But you do need whitespace of some kind in some cases.

    Also, it seems like ' and " are interchangeable

    Yes, they are. I tend to standardize on double quotes to make it easier to read quickly. But if you need to have a double quote " in your string, then often easier to type '"test"'

Answers

  • Kevin_579059
    Options

    Thank you, that worked! Two follow-up questions: So does the formatting really matter? For example, having the closing brace } of the previous if be on a new line and directly in front of the next else if ?
    Also, it seems like ' and " are interchangeable, for example in fields['first-alpha-component'] and fields["first-alpha-component"] . . . for me both of these had the same function (both worked).