Case()

Category: Logical

Syntax: Case ( test1; result1 {; test2; result2; defaultResult...} )

Parameters:

test(n)An expression that yields a Boolean result.

result(n)The value to return if corresponding test is true.

defaultResultThe value to return if all tests are false. Parameters in curly braces { } are optional and may be repeated as needed, separated by a semicolon.

Data type returned: Text, Number, Date, Time, Timestamp, Container

Description:

The Case function returns one of several possible results based on a series of tests.

Each test expression is evaluated in order, and when the first true expression (one that resolves to a Boolean 1) is found, the value specified in the result for that expression is returned. The function stops evaluating as soon as it finds a true test.

The default result at the end of the parameter list is optional. If none of the tests evaluate to True, the function returns the value specified for defaultResult. If no default result is specified, the Case function returns an "empty" result. If you believe that one of the tests in the Case should always be true, we recommend using an explicit default case, possibly with a value of "default" or "error" to assist in error trapping.

Consider using hard returns in long Case() statements to make them more readable, and indent lines with tabs, as shown previously. (Note that this example makes repeated calls to Get(SystemLanguage); in practice it might be better to use Let() to make a single call to Get(SystemLanguage) so that it needs to be evaluated only once.)

In the last example, while all three tests resolve to true, only the first line is executed and its result returned. Using the Case() function, with its "short-circuiting" feature, can help with performance tuning.

Examples:

Function

Results

Case ( IsEmpty (Contact_Name ); 1 )

Returns 1 if the Contact_Name field is empty.

 

Note that a default value is not required, making the usage of Case() shorter than If().

Case ( Get(SystemLanguage) = "English"; "Welcome"; Get(SystemLanguage) = "French"; "Bienvenue"; Get(SystemLanguage) = "Italian"; "Benvenuto"; Get(SystemLanguage) = "German"; "Willkommen"; Get(SystemLanguage) = "Swedish"; "Välkommen"; Get(SystemLanguage) = "Spanish "; "Bienvenido",; Get(SystemLanguage) = "Dutch"; "Welkom"; Get(SystemLanguage) = "Japanese"; "Irashaimasu" ; "Sorry... not sure of your language." // default value )

Returns a welcoming message in the language determined by the Get (SystemLanguage) function.

Case ( SalesTotal < 10; .1; SalesTotal < 50; .2; SalesTotal < 100; .3; .35 )

Returns .1 when the value in the SalesTotal field is 5, and returns .2 when the value in the SalesTotal field is 12.

Категории