OR Function in Salesforce

In this Salesforce tutorial, we will learn about the OR function in Salesforce. We will discuss the use case and execution and use case of the OR function with the help of examples in Salesforce Lightning and Salesforce Classic editions respectively.

What is OR function in Salesforce

In Salesforce, the OR function is a logical function that determines if expressions are true or false, it returns TRUE if any expression is true and in the same way, returns FALSE if all expressions are false.

We can also use the OR function in combination with other logical functions, such as AND, to create more complex conditions.

Syntax for Salesforce OR function

The syntax for the Salesforce OR function is mentioned below.

OR(logical1, logical2...)

In the above syntax, (logical1, logical2..) are the conditions or expressions that you want to evaluate. We can extend and specify these logical conditions up to 1,000 conditions.

Considerations:

There are a few considerations to follow while using the OR function in Salesforce formulas.

  • There is a limit to the number of logical conditions up to 1000.
  • Make sure that the data types of the conditions being evaluated are compatible with the OR function. This function works with Boolean (true/false) values.
  • Enter the logical conditions in the correct order within the OR function. The function stops evaluating as soon as it encounters the first true condition.
  • We can also use nested logical functions like AND within an OR function to create more complex conditions.

Use OR function in Salesforce Lightning

In this example, we will create a validation rule in the Opportunity object with the help of the OR function. This validation rule ensures that opportunities cannot be closed unless certain criteria are met.

In this scenario, the criteria is that an opportunity can only be closed if either the “Contract Signed” checkbox is checked or the “Approval Received” checkbox is checked.

To create a validation rule in Salesforce Lightning, navigate to the Setup page of Lightning and follow the below steps.

1. On the Setup page of Salesforce Lightning, click on the Object Manager tab.

2. In the Object Manager, select the Opportunity object or you can select the object in which you have to create the validation rule.

3. In the Opportunity setup click on the option Validation Rule in the left sidebar then in the next window click on the New button.

Salesforce OR function validation rule

4. In this window we will define the validation rule, and for that first enter the field Rule Name. In this example, I have entered the field Rule Name as Closing Opportunity Validation.

Create a validation rule using the OR function

5. In the section Error Condition Formula, we will enter the below formula in the formula editor.

AND(
    ISPICKVAL(StageName, 'Closed Won'),
    NOT(
        OR(
            Contract_Signed__c,
            Approval_Received__c
        )
    )
)

The condition that we applied in the above formula is as follows:

  • ISPICKVAL(StageName, ‘Closed Won’) checks if the Opportunity’s Stage Name is set to ‘Closed Won’. It returns true if the stage is ‘Closed Won’.
  • The OR function checks if either the Contract_Signed__c, checkbox or the Approval_Received__c checkbox is checked. If at least one of them is checked, it returns true.
  • The NOT function inverts the outcome of the OR function, and returns true only when both Contract Signed and Approval Received are unchecked.
  • The AND function combines the two conditions. It returns true only if both conditions are satisfied – the stage is ‘Closed Won’, and neither the Contract Signed nor the Approval Received checkbox is checked.

6. After entering the formula, click on the button Check Syntax to validate the entered formula.

Salesforce OR function use case

7. After entering the above formula, go to the field Error Message and enter the message that will appear on the screen when the error condition for the Validation rule is true.

After entering the error message click on the Save button.

Salesforce lightning Validation formula using OR function

Since we have created this validation rule in the Opportunity object, I will open an opportunity with the Closed Won stage and there I will leave the fields Contract Signed and Approval Received as unchecked.

OR function in Salesforce

As we can see in the above image when we left both the fields unchecked in a closed won opportunity we got an error message on the screen. Hence the error condition that we have defined in the validation rule is true.

In this way, we have successfully executed the OR function in Salesforce Lighting by creating a Validation Rule.

Use OR function in Salesforce Classic

In this example, we will use the OR function to create a validation rule in the Case object to track the case resolutions. This validation rule will ensure that cases cannot be marked as “Closed” unless either the “Solution Provided” checkbox is checked or the “Customer Confirmed Resolution” checkbox is checked.

To create a Validation rule in Salesforce Classic, follow the below steps.

1. On the Home page of Salesforce Classic, click on the Setup tab that is on the upper right side of the screen.

2. In the Setup page, go to the Build section in the left sidebar then click on the Customize dropdown.

In the Customize dropdown click on the Case object then select Validation Rules.

Salesforce Classic OR function use case

3. In this step, click on the New button to create a validation rule.

4. In this window, we will specify the validation rule by defining an error condition and error message but before that enter the name of the validation rule in the Rule Name field.

In this case, I have named the validation rule as Closed Case Validation.

Create a validation rule using the OR function in Salesforce Classic

5. In the section Error Condition Formula, enter the below formula in the formula editor.

AND(
    ISPICKVAL(Case_Status__c, 'Closed'),
    NOT(
        OR(
            Solution_Provided__c,
             Resolution_Confirmed__c 
        )))
  • ISPICKVAL(Status, ‘Closed’), checks if the Case Status field of the Case is set to ‘Closed’. It returns true if the case is in the ‘Closed’ status.
  • The OR function checks if either the ‘Solution Provided’ checkbox or the ‘Resolution Confirmed’ checkbox is checked. If at least one of them is checked, it will return true.
  • The NOT function reverses the outcome of the enclosed expression, which, in this instance, is the result of the OR function. It evaluates to true only when both the ‘Solution Provided’ and ‘Customer Confirmed Resolution’ checkboxes are unchecked.
  • The AND function combines the two conditions of the formula. It returns true only if both conditions are satisfied.

6. After entering the above formula in the formula editor click on the button Check Syntax to validate the formula.

Custom validation formula using OR function in Salesforce

7. After entering the formula, go to the text area field Error Message and enter the message that will appear on the screen if the error condition is true according to the applied condition.

After entering the error message click on the Save button.

Salesforce Classic OR function execution

The validation rule is created in the Case object, so I will open a Case and keep the Case Status as Closed and there I will leave the fields Solution Provided and Resolution confirmed as unchecked.

OR function in Salesforce Classic

As we can see in the above image when the checkbox fields Solution Provided and Resolution Confirmed were left unchecked in a closed case the error condition became true for the validation rule and we can see an error message on the screen.

In this way, we have successfully created a validation rule in Salesforce Classic with the help of the OR function.

Conclusion

In this Salesforce tutorial, we have discussed the use case of the OR function in Salesforce Lightning and Salesforce Classic. In the above examples, we have successfully created validation rules using the OR function.

In addition to this, we also learned about the use of the OR function in Salesforce and learned the syntax of the OR function, later we discussed the considerations that we should be aware of while using the OR function in Salesforce.

You may like to read: