Salesforce IF Function

Do you want to learn about Salesforce IF Function and how it works? In this Salesforce tutorial, you will learn completely about the IF Function with syntax, use cases, considerations, and examples.

Moreover, you will explore step-by-step how to use the IF Function in the formula field in Salesforce Lightning and Salesforce Classic Interface.

What is IF Function in Salesforce

The Salesforce IF function is one of the logical functions that helps you to make logical decisions based on specific conditions. The IF function in Salesforce is used to determine if expressions are true or false.

The Syntax of the IF function is as given below:

IF(logical_test, value_if_true, value_if_false)

Here:

  • logical_test: The logical_test parameter is the condition that you want to evaluate. In simple terms, it is a logical expression that returns results in true or false.
  • value_if_true: The value_if_true parameter is the value or expression to be returned if the logical_test parameters are evaluated to be true.
  • value_if_false: The value_if_false parameter is the value or expression to be returned if the logical_test parameter is evaluated to be false.

If you talk about the return of the IF function, it returns the first value if TRUE and another value if FALSE after checking whether a condition.

Additionally, the IF function is commonly used for conditional formatting where you want to assign something based on some criteria.

To deeply understand it, let’s take an example: Suppose you want to assign a rating to the customer care executive based on the feedback scores by the customers.

You want to work it like that:

Customer Feedback ScroreRating
Customer_Feedback_Score__c greater than or equal to 4.Satisfied
Customer_Feedback_Score__c greater than or equal to 2.Neutral
Customer_Feedback_Score__c does not match any of the above conditions.Unsatisfied

The formula for the above table using the IF function looks as follows in Salesforce:

IF(Customer_Feedback_Score__c >= 4, 'Satisfied', IF(Customer_Feedback__Score__c >= 2, 'Neutral', 'Unsatisfied'))

Furthermore, there are some considerations that you must take care of while working with the IF function in Salesforce:

  • Make sure that the value_if_true parameter and value_if_false parameter are of the same data type in the Salesforce IF function.
  • While using the IF function to determine the Salesforce user license of the currently logged-in user with the $Profile.UserType variable, make sure that it has the following values:
    • Standard for Salesforce
    • PowerPartner for PRM User
    • CustomerSuccess for Customer Portal User
    • PowerCustomerSuccess for Customer Portal Manager

With this, you have learned about the IF function, its syntax, use case, example, and its considerations in Salesforce. Now, you will explore how to use the IF function in the formula field in Salesforce Lightning.

How to use IF function in Salesforce Lightning

You will be guided step-by-step on using the IF function in the formula field in the Salesforce Lightning Interface with the help of an example.

Suppose you want to divide the accounts into different tiers based on the annual revenue as defined below:

Annual Revenue of AccountTiers
Annual Revenue greater than or equal to 10000000.Tier 1
Annual Revenue greater than or equal to 5000000.Tier 2
If none of the conditions are satisfied. Tier 3

The following are the steps to achieve the desired result using the IF function in Salesforce:

See also  ATAN2() Function in Salesforce

1. Log in to the Salesforce account as an administrator. Click the “Gear” icon in the upper right corner and choose “Setup” from the dropdown menu if you are not already on the “Setup Home” page.

2. From the navigation bar, click the “Object Manager” tab.

Utilize the “Quick Find” box to look for the object on which to use the IF function. You can look for the “Account” object in accordance with the situation.

Salesforce IF Function

3. Click on the “Fields & Relationship” option, and click on the “New” button located in the upper right corner of the page.

Salesforce IF Function Example

4. Now, choose the data type to specify the type of information that the custom field will contain by checking the radio button given next to it.

As you want to use the function in the formula, choose the “Formula” data type, and to proceed, click on the “Next” button.

IF Function in Salesforce

5. Enter the name you want displayed for that field in the “Field Label” box. After that, click on the “Field Name”; when you click on that, it automatically fills.

According to the scenario, here you can specify the field label as “Tiers”.

Navigate down and choose a data type from the list that matches the return type of your formula. As per your scenario, you need to select the “Text” data type as the formula’s return type.

Click on the “Next” button to proceed to the next step.

IF Function in Salesforce Example

6. Once you click on the next, you will be landed on the add formula page. Move to the right side of the page. Here, you will get the “Functions Section,” and here is the dropdown field “All Function Categories”, click on it, and from here, choose “Logical“.

Once you click on the logical, you will get the list of the functions that are in the logical categories; now, from here, you have to choose the “IF” function and enter it into the advanced formula editor, as our topic is to understand how to use the IF function.

Replace the parameters of the function with the merger fields, expressions, or functions as per the requirement of the scenario.

For example, you want to classify the accounts into different tiers based on their annual revenue as per the defined scenario. The formula is as follows:

IF(AnnualRevenue >= 10000000, 'Tier 1', IF(AnnualRevenue >= 5000000, 'Tier 2', 'Tier 3'))

Here is a detailed explanation of the formula:

  • IF function: It is a conditional function used to make decisions based on specified conditions. The conditions are as follows:
    • Condition 1: AnnualRevenue >= 10000000: If this condition is true, the result is ‘Tier 1’.
    • Condition 2: AnnualRevenue >= 5000000. If Condition 1 is false but Condition 2 is true, the result is ‘Tier 2’
    • Default: If both Condition 1 and Condition 2 are false, the result is ‘Tier 3’.

Click on the “Check Syntax” button to validate the formula and ensure that there are no errors.

Optionally, you can “Handle the empty field” as well as you can define the “Description” and “Help text” if you want.

See also  Salesforce ISOWEEK Function

Click on the “Next” button to move to the formula field-level security step.

Salesforce Lightning IF Function

7. In this step, choose the user profiles to whom you want to grant the field-level security edit access to the field. If not added the field level security, the custom formula field that we have created does not show on the unselected profile.

After that, click on the “Next” button and move to the next step.

Salesforce Lightning IF Function Example

8. Choose the “Page Layout” where you want to add the custom formula field. If you do not select a layout, the field will not appear on any pages. And last, to save the advanced formula field, click on the “Save” button.

How to use IF function in Salesforce

9. Once you have created the formula field, you can use this. Let’s look at an example:

  • Open the “Account” item, create a new account with the “Annual Revenue” field, and save it.
  • Once you have saved the account, you will move to the “Detail Section”, where you will get the field “Tiers” that shows you the result of the IF function.

Let’s understand it with the example:

  • Suppose you have created an account with annual revenue of “12000000” so that the IF function will execute and assign the tiers as “Tier 1”.
IF Function in Salesforce Lightning Example

With this, you have learned how to use the IF function in Salesforce Lightning. Now, learn how to use the IF function in Salesforce Classic.

How to use IF function in Salesforce Classic

The following are the steps to use the IF function in Salesforce Classic.

Let’s take an example to understand it: Suppose you want to set the renewal reminder based on the expiration date of a contract.

1. Login to the Salesforce account in Classic Experience. Select “Avatar” and then “Switch to Salesforce Classic” from the dropdown menu if you are not currently logged in to Classic.

2. Click on the “Setup” option at the top right side of the page between your name and the help option.

Salesforce Classic IF Function

3. Move downward to the “Build” section, click on the “Customize” dropdown, and choose the object from the list of the Salesforce objects on which you want to use the IF function.

As per the above-defined scenario, I searched and selected the “Contract” object.

As you click on the object dropdown, you will get multiple options related to the object. From here, choose the “Fields” option, as your requirement is to create a field where you can use the Salesforce function.

Salesforce Classic IF Function Example

4. Scroll down to the “Custom Fields & Relationship” section and click on the “New” button.

IF Function in Salesforce Classic

5. It will redirect to the “Choose the field type” page, now check the radio button in front of the “Formula” data type. To proceed to the next step, click on the “Next” button.

IF Function in Salesforce

6. Enter the “Field Label” for the formula, and the unique “Field Name” will be automatically populated. In this case, I entered the field label named “Renewal Reminder”.

Choose the data type that will store our return result as per the requirement of the formula; you need to pick the “Text” data type.

Click on the “Next” button to move to the next step.

IF Function in Salesforce Classic Example

7. Click on the “Advance Formula” subtab to enter the formula with the use of additional fields, operators, and functions.

See also  Salesforce TIMEVALUE Function

After that, move to the “Function Section” select “All Function Categories” -> click on “Logical” -> search, and select “IF” -> click “Insert Selected Function”.

If you require more functions, you can add them similarly, and to add the fields and operators, click on the “Insert Field” button and “Insert Operator” select, respectively.

For example, here, you want to set the renewal reminder based on the expiration date of a contract. Here is the formula given below:

IF(TODAY() >= Expiration_Date__c - 30, 'Renewal Due', 'No Action Needed')

Here is a detailed description of the formula:

  • TODAY(): It is a function that returns the current date.
  • Expiration_Date__c: It is a custom field that stores the expiration date of the contract.
  • Expiration_Date__c – 30: Here, we subtract 30 days from the expiration date.
  • IF() Function: It is used to work according to the evaluation of the condition. We are checking If the current date is equal to or later than 30 days before the expiration date.
    • Result if TRUE: If the condition is true, it returns ‘Renewal Due’.
    • Result if FALSE: If the condition is false, it returns ‘No Action Needed’.

Click “Check Syntax” to review the formula and ensure there are no errors. Define the “Description” and “Help text” as well as “Handle the empty field” if you would like.

Click the “Next” button to move to the next step.

How to use IF function in Salesforce Classic

8. In this step, you have to set up the “Field Level Security”. Here, you will need to select the profiles for which you want to grant edit access to this field.

To proceed, click on the “Next” button.

How to use IF function in Salesforce Classic Example

9. This step adds this custom field to the page layout. Select the page layout names where you wish to include this field. By default, all page layouts are selected; if you want to not include this custom field on any specific page layout, uncheck it.

Otherwise, click on the “Save” button to complete the process of creating a formula field.

How to use IF function in Salesforce

10. Once the formula field is created, you can use the formula field. Let’s see an example:

  • Open the “Contract” object, create a new contract with “Expiration Date”, and save the contract.
  • Once you save, move to the contract detail page. Here, you will get the “Renewal Reminder” field that shows you the result of the IF function.

Let’s deeply understand it with the example:

  • Suppose you have created a contract with the expiration date “01/02/2024” and today’s date “01/15/2024” and save it. Once the formula is executed, it gives the result for renewal reminder “Renewal Due”.
Example of IF Function in Salesforce

With this, we have learned how to use the IF function in Salesforce Classic.`

Conclusion

In conclusion, we have about the Salesforce IF function. Moreover, we have covered the in-between topics of the IF function, such as its syntax, use case, example, and considerations.

Additionally, we have explored step-by-step how to use the IF function in Salesforce Lightning and Salesforce Classic.

You may like to read: