9 Replies Latest reply on May 11, 2016 11:48 PM by user19752

    Case Statement

    lmarcinelli

      In a script, can I use a case statement instead of a nested if?  For example:

       

      if [ $WhichOne = 1]

                Perform Find

                Set Field

      Else  if [ $WhichOne = 2]

                Perform Find

                Set Field

      Else  if [ $WhichOne = 3]

                Perform Find

                Set Field

      End if

      End if

      End if

       

      ___________________

       

      Case $WhichOne of

      1:  Perform Find; Set Field

      2:  Perform Find; Set Field

      3:  Perform Find; Set Field

      end case

       

       

       

      Thank you, in advance.

        • 1. Re: Case Statement
          patricia

          Yes

          • 2. Re: Case Statement
            ErikWegweiser

            Yes. And you've almost got it, except there's no need to close with three "End If" steps. Only one is required. Furthermore, you can add an "Else" step after the last "Else If" as a fall through when none of the other tests are true.

            • 3. Re: Case Statement
              Mike_Mitchell

              There is no Case (or equivalent) script step. There is a Case function, but that's a differently pigmented equine.

               

              One thing you can do, in some cases, is substitute the Case function to achieve the result you want. You can use script steps that Set Field or Set Field By Name, and then use the criteria dynamically.

               

              But no, you can't use a switch or choose as exist in some other platforms.

              1 of 1 people found this helpful
              • 4. Re: Case Statement
                beverly

                in script (when you add these), you automatically get the End if after the if. Just insert the Else IF (or Else) as needed.

                 

                If ( $x=1 )

                     ...

                Else If ( $x=2 )

                     ...

                Else If ( $x=3 )

                     ...

                Else

                     ...

                End If

                 

                NOTE: I change the square brackets to parens, because reply by email will chew them up and spit them out!

                beverly

                1 of 1 people found this helpful
                • 5. Re: Case Statement
                  lmarcinelli

                  Thanks Mike, that is what I thought. 

                  I get so frustrated with Filemaker. I haven't wrote a Nested If since Cobol!

                  thanks again.

                  • 7. Re: Case Statement
                    Extensitech

                    As noted, there's not a "case" script step.

                     

                    You my find the technique at the end of this article useful, though: Designing for Extensibility - Extensitech | Extensitech

                     

                    (For similar ideas, see Calculation Function Formatting | Extensitech)

                     

                    Basically, you can leave the if blank and put all your conditions in else if's. It doesn't change behavior, but it makes it easier to duplicate or move the first condition, and essentially works like a case.

                     

                    Pro-tip: to get the (somewhat over-eager) type-ahead to "let go" of the if entry, hit escape twice.

                     

                    Chris Cain

                    Extensitech

                    • 8. Re: Case Statement
                      keywords

                      An If, Else If script sequence in a script is essentially exactly the same as a Case calc. In both of these FM goes through each option until it finds one that fits, executes that and ignores the rest. The only difference really is the way they are written—in a script the action is another line of script; in a calc the action follows a semi-colon. And as already pointed out, i both instances you can have a default action—in a script it is preceded by Else, in a calc it sits on its own line, but in both it is action which is performed if no previous condition has been enacted.

                      1 of 1 people found this helpful
                      • 9. Re: Case Statement
                        user19752

                        In addition to manys already wrote...

                        If you use "Else If", you need only one "End If", so this is not "nested", totally using one If block.

                         

                        Nested one is something like

                        If [cond1]

                          //job for 1

                        Else

                          If [cond2]

                            //job for 2

                          Else

                            If [cond3]

                              //job for 3

                            Else

                              //job for else

                            End If

                          End If

                        End If