Salesforce AND Function

One such often-used function in Salesforce is the AND() function, which is used to parse two or more than two functions in a single formula field. In Salesforce, we use functions for customizations, building business logic, and defining validation rules.

The AND() function is executable in Salesforce Lightning as well as Salesforce Classic edition.

What is AND() function in Salesforce

In Salesforce AND() function is a logical function that is used in formula fields, validation rules, and workflows, where we need to evaluate multiple conditions and ensure that all of them are TRUE.

The AND() function can take multiple functions as arguments and return TRUE when all the conditions in the function are TRUE if a single condition with the function is FALSE it will return FALSE.

Syntax for Salesforce AND() function

The AND() function combines multiple conditions, and the syntax for this function is written in the following way.

AND(logical1, logical2, logical3, ...)

In the above syntax, the logical1, logical2, logical3, etc are the conditions or expressions that you want to combine. Each condition must be a logical expression(field or function) that returns either TRUE or FALSE.

Considerations:
There are some considerations that we should be aware of while executing the AND() function in Salesforce.

  • Even if one condition is False in the function it will return False.
  • All conditions need to be TRUE to return True.
  • While using this function in the Validation rule we need to ensure that certain conditions are met before a record is saved.

How to use the AND() function in Salesforce Lightning

In the below steps, we will discuss the use case for the AND() function in Salesforce Lighting by executing this function in the validation formula of a Salesforce object.

1. Navigate to the Setup page of Lightning and for that, click on the settings icon on the upper right of the screen and select the option Setup.

AND function use case in Salesforce Lightning

2. In the Object Manager, select an object to create a validation rule using the AND function. In this example, I have selected the object Account.

3. In the object setup click on the Validation rules on the left sidebar, then click on the New button.

4. In this step, we will define the Validation rule by entering the error condition formula using the AND function.

  • In the Rule Name field, enter a relevant name for the Validation rule. In this example, I have named it as a New Account rule.
  • Go to the section Error Condition Formula then select the AND function from the Functions scroll bar that is on the left side.
  • After selecting the function click on the button Insert Selected Function.
AND function Validation rule in Salesforce

5. In the syntax of the AND function replace the expressions Logical1, and Logical2 with the functions available in the object.

In this example, I have selected the functions ISBLANK() and ISNEW().

6. Since the function ISBLANK() requires a field as an expression and to add a field click on the button Insert Field.

Insert field in the AND function in Salesforce Lightning

7. Now the formula in the formula editor will appear as below code.

AND(ISBLANK (Annual Revenue) , ISNEW())

In the above formula, two functions ISBLANK() and ISNEW() are used as logic for the AND function. This means when the Annual Revenue field of a New record will be left blank the error condition will become true.

The error message will appear when both the conditions of the formula return as TRUE.

8. After entering the formula click on the button Check Syntax to validate the formula.

On successful validation, enter the error message that will appear on the screen when the error condition is true according to the formula.

After entering the error message click on the Save button.

Salesforce Lightning AND function use case

As we click on the Save button, the validation rule will be activated in the selected object.

The validation rule that we have created using the AND() function will be true according to the applied formula when we create a new record with a blank Annual Revenue field.

Now I will create a new record in the object to check whether the validation formula that we created using the AND() function is working or not.

Salesforce Lightning AND function use case

As we can see the error message in the above image appeared when the condition of the validation rule became true. i.e. when we left the field Annual field blank (ISBLANK) while creating a new record (ISNEW) the condition for the error message became true.

In this way, we have successfully executed the AND() function in Salesforce Lightning with the help of the above steps.

How to use the AND() function in Salesforce Classic

Now we will discuss the use case of the AND() function in Salesforce Classic. In the below step, I will explain the execution of the AND() function with the help of creating a validation formula in an object.

1. Go to the setup page of Classic, and for that click on the Setup tab that is on the upper right corner of the window.

2. On the Setup page of Salesforce Classic, go to the Build section click on the Customize dropdown, and click on the object in which you have to create a validation rule.

In the dropdown of the object select the option Validation Rules.

Salesforce Classic AND function use case

3. In the setup window of Validation Rules, click on the New button.

4. In this window, we will define a validation rule by specifying an error condition and a corresponding error message.

First, enter a relevant name for the Validation Rule in the Rule Name field then go to the Functions scroll bar on the left-hand side select the AND function, and click on the button Insert Selected Functions.

Create a Validation rule in Salesforce using AND function

5. In the Formula field we can the syntax of the AND function as :

AND(logical1,logical2,...)

In the syntax of the AND function, there are expressions such as logical1 and logical2, and these expressions should be replaced with two or more than two functions.

In this example, I have added two functions ISBLANK() and ISNEW() which means the error message will become true if the field in the new record is blank.

Now click on the Insert field button to add an expression in the field if required.

Insert field in the AND function in Salesforce Classic

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

On successful validation write the message in the Error Message field and this message will display when the formula condition of the validation rule will return as True.

At last click on the Save button.

Salesforce Classic AND Function Validation Rule

As we click on the Save button the validation rule will be created in the object that you have selected in the above steps.

Now go to the object in which you have created this validation rule to check whether it is working or not.

In this example, I have created this rule in the Leads object, and according to the formula defined, the error message condition will be true if I create a new lead record without entering the field City.

Salesforce Classic AND Function execution

As we can see in the above image the error formula is displayed on the screen when the field is left blank in the new record.

In this way, we have successfully executed the AND() function with the help of the Validation rule in Salesforce Classic.

Conclusion

In this Salesforce tutorial, we have learned about the AND() function using a Validation formula and we successfully executed the function in the Salesforce Lightning and Salesforce Classic.

Along with this we also learned about the use of the AND() function and the syntax and considerations that we should be aware of while using the AND() function.

You may like to read: