Home      Ask a Question      My Stuff      Help   
  
TimeWorks: Scripting - IF Statement - Using AND & OR
Question
ID
464
Category
Training
  FAQ's
Date Created
2/8/2010 6:56:21 AM
Date Updated
8/13/2014 3:03:40 PM
Can I set a script to identify an employee, using a certain field in (Employee Settings), and their use of a certain department number in a (clock) prompt, to associate those hours with a particular Record Type (Category/Job Code)?
Answer
This article will explain how to use "AND" and "OR" to test for multiple conditions or identifiers in the same IF statement.
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

The system will evaluate the condition in the IF statement to see if it is TRUE (run the script) or FALSE (skip the script). When multiple conditions must be evaluated, they can be combined in one IF statement using AND and OR. These mean the same thing in scripting as they do in English. Some examples of usage can be found below.


AND
¯¯¯
Use AND when ALL of the conditions must be evaluated as true, for the script to apply the change. Example scenario: when an employee works on a holiday, the category should change to "Holiday Worked", but you only want to make that category change if the punch is in the "Regular" category. Example script:

     if (isholiday = true and category = "Regular")
     {
     category = "Holiday Worked";
     }


Here is another example of using multiple AND's, but this time, to test for three conditions:

     if (category = "Regular" and department = "100" and x = "01")
     {
     category = "Rate 1";
     }

So in the above example, all conditions must be true for the change to be applied. The category will change from "Regular to "Rate 1" when: the punch is in "Regular" category (which is usually the default), the Department field (in Employee Setup) is "100", and the x-prompt is "100".


OR
¯¯
Use OR when you want the script to apply the change when ANY of the conditions are evaluated as true. Example scenario: the punches from either, Location "East Building" or "North Building", should be paid at $12.00 per hour. Example script:

     if (location = "East Building" or location = "North Building")
     {
     payrate = 12;
     }


Here is another example of using multiple OR's, but this time, to test for three conditions:

     if (location = "East Building" or location = "North Building" or department = "Front Office")
     {
     payrate = 12;
     }


Combining AND and OR in The Same IF Statement
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Use a combination of AND and OR when there are three or more conditions and one condition must be true, in addition to either one of the other two conditions. Example scenario: the punches from employees in department "100" and from either, Location "East Building" or "North Building", should be paid at $12.00 per hour. Example script:

     if (department = "Front Office" and (location = "East Building" or location = "North Building") )
     {
     payrate = 12;
     }

Notice that there is an extra set of parentheses in the above IF statement. The inside set of parentheses tells the system to evaluate the enclosed conditions as a group. If the parentheses were arranged differently the script would run differently. In the next script everyone in North Building and only the employees in East building in department "Front Office", will get 12 per hour. Example Script:

     if ( (department = "Front Office" and location = "East Building") or location = "North Building" )
     {
     payrate = 12;
     }


Keep in mind that the each condition must be written out entirely, so it can be evaluated as true or false. Meaning this will not work:

     This will NOT work --> if (Department = "100" or "200")

The system would stop that script when it saw "200" because, that cannot be evaluated as true or false.

For more information, please see the other "IF Statement" articles below.
Back to Search Results