if, if...else, if, if else Statements

if Statements

Scripting in both TimeWorks and TimeWorksPlus relies greatly on if statements as well as else statements.  Very simply, an if statement can be used, by itself, to test a condition and do something based on that test.

In plain English, when you write an IF statement, you are basically writing, "if _______ is true, then do _______", in a way that the computer can understand.

All "IF" statements contain a pair of parentheses "()" and a pair of braces "{}". The following example will show this is written without any keywords. (You can actually use this example to start every IF statement you will ever write, then just fill in the rest.)

     if(...)
         {...}

To actually use this in a script, you will fill in the parameters and commands that will accomplish the custom processing that is needed. The following example shows what each part of the IF statment does:

     if ( statment that can be evaluated as "TRUE" or "FALSE" ) { Commands that should run when the IF statement is TRUE }

For instance:

if(employee.department = "400"){
     Dept = "Concessions";
}

if, else Statements

The scripting language can also include the use of "else" statements. Meaning, if it meets a requirement then do something and if it does not do something else.

An example of the syntax that uses the else statement is listed below:

if(employee.department = "400"){
     Dept = "Concessions";
}
else{
     Dept = "Other";
}


In this example, if the Department is equal to 400, then it will change the Dept prompt (on the time card) to "Concessions". If the department is anything other than 400 then Dept is set to "Other". Note that unlike the if statement, the else statement does not have a condition that needs to be met for Dept to be set to "Other". This is because else applies for everything that does not meet the prior if condition.

if, else if, else Statements

A series of conditions can be tested by adding else if statements. These are arranged in a sequence with an initial if followed by else if statements. If all those statements prove to be false, the script will look for the else statement.

if(employee.department = "400"){
     Dept = "Concessions";
}
else if(employee.department = "500"){
     Dept = "Ticket Sales";
}
else if(employee.department = "600"{
     Dept = "Clubhouse";
}
else {Dept = "Other";
}


This script checks a series of conditions, but once it finds a true statement, it ignores all the subsequent lines of the script. This makes the script more efficient as it stops checking conditions that it doesn't need to check. So for instance, once we know the department is 400, we don't need to check if it is 500, 600 or anything else.

IMPORTANT: Due to the way our servers process scripting, it is best to keep else if chains to ten clauses or less. Anything beyond that should be scripted with translate or a series of if statements.

Must Have "Else"
If you ever see the following error in the Activation Status column of the Processing Rules

FAILURE: INDEX WAS OUT OF RANGE. MUST BE NON-NEGATIVE AND LESS THAN THE SIZE OF THE COLLECTION. PARAMETER NAME: INDEX

it is because you have an "if...if else..." series of statements without an ending "else" statement. If you have addressed all your conditions, you can put in a dummy else statement to eliminate this error:

else{$donothing = 0;}

Nesting if Statements

This section will explain how to use multiple levels, or tiers, of if statements.

This is useful when you need to apply a custom rule to a very specific group of employees. In the following examples we will start with a one "level" script and then add levels to it.

In the first example, lets say you wanted the group of employees in the "Office" location to get a pay differential, you could write this: 


if (employee.location = "Office") {
   payrate = payrate + 1.00;
}


Now we will add an additional level of IF statements, that will specify the differential amount, based on the Employee's Home Supervisor:


if(employee.location = "Office"){
          if(supervisor = "John"){
               payrate = payrate + 1.00;
          }
          if(supervisor = "Amy"){
               payrate = payrate + 2.00;
          }
}


This script uses two consecutive if statements for the supervisor portion of the script. That means the script will check to see if the supervisor is Amy even if the supervisor has already been determined to be John. If the employee has neither person as a supervisor, nothing will change with their pay rate.

 

Did you find this article helpful?