and, or, within Statements

This article will explain how to use AND and OR in scripting to test for multiple conditions or identifiers in the same if statement.

It will also cover the use of within as an alternative to multiple or statements.

In the case of each of these words, the system will evaluate the condition in the if statement to see if it is either 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. 

AND

Use and when ALL of the conditions must be evaluated as true. If all conditions are true, then the script will execute the commands.

For example, 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 as well.

So, in order to change hours to Holiday Worked, two conditions must be met:
  • The day of the punch has to be a holiday
  • The category of the punch has to be regular
This would be scripting with an and statement in the following manner:

     if (reportingdate.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.
  • Category has to be Regular
  • Employee must be in home department 100
  • A Job Code clock prompt has to be equal to "01"
     if(category = "Regular" and employee.department = "100" and JobCode = "01"){
         category = "Rate 1";
     }


So in the above example, all conditions must be true for the change to be applied. 

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 of the following locations, "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;
 }

IMPORTANT

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.

"within" Function

There is also a function in scripting that can take the place of using several OR's in the IF expression. This can be very helpful when there are many values that must be strung together with an OR. For example, if you were scripting an action based on an employee being in one of 4 departments, you could use:

if(employee.department = "100" or employee.department = "110" or employee.department = "200" or employee.department = "210"){...}

However, this starts to get somewhat cumbersome and difficult to follow. Also, since the system will only allow 20 "OR"'s in one IF expression. For those reasons, it may be better to use the WITHIN function.

The WITHIN function returns either TRUE or FALSE if the object being checked is part of the list. Keep in mind that WITHIN will be TRUE if the item you are comparing matches ANY of the values in the compare string.

Here is how to use WITHIN:

The first item will be the variable you wish to compare (like: department, location, supervisor, home1, X, Y, Z prompts); then a "|" (pipe symbol found above the enter button on most keyboards) separated string of values you wish to check it against.

The entire string will need to be surrounded by "".

Note: Do not use spaces, unless the value you are comparing actually does have spaces.

     within(ItemToCompare, "string | of| values | to | compare" )

Here is a working example:

     if(within(employee.department, "100|110|200|210") = TRUE){
      payrate = 15;
     }


It might help to think of the above example like this, "IF the employees department matches one of these values: 100 | 110 | 200 | 210 THEN do something..."

Did you find this article helpful?