In this Salesforce article, we will learn about the Salesforce NOT function. It is an inbuilt function often used in formula fields, validation rules, and workflow rules.
Additionally, we will discuss a real-time example where we will use the NOT function to create a validation rule in Salesforce Lightning and a formula field in Salesforce Classic.
What is NOT function in Salesforce
In Salesforce, the NOT function is a logical function used in formulas and expressions to negate or reverse the logical value of a given condition. It returns the FALSE for TRUE or TRUE for FALSE.
This function is also used with other logical functions, such as AND and OR, to create more complex conditions in a formula. The NOT function is generally useful in defining complex conditions where we need to construct conditions based on multiple factors.
Syntax of NOT function in Salesforce
The Syntax of the NOT function is written in the following way.
In the above syntax, logical expression is supposed to be replaced with the expression that you have to evaluate to be either true or false.
There are some considerations to using the NOT function in Salesforce.
- The NOT function works with a logical expression, which should be evaluated as either true or false.
- The NOT function is generally used with other logical functions such as AND and OR functions to define complex conditions.
- Avoid using the NOT function with picklist and text fields instead use the ISBLANK or ISNULL function for that.
- Use a suitable field as a logical expression for the NOT function to avoid errors.
How to use NOT function in Validation Rules in Salesforce Lightning
In this example, we will create a validation rule for the opportunity object in Salesforce lightning and there we will specify the condition of the validation rule with the help of the NOT function.
The custom validation rule will ensure that this field can only be updated when the Opportunity Stage is “Prospecting”. If the Opportunity Stage is anything other than “Prospecting”, the update to the “Probability” field should be restricted.
To create a custom Formula field in Salesforce Lightning, follow the below steps.
1. Navigate to the Setup page of Salesforce Lightning, and for that click on the Settings icon on the upper left side of the window, and in the dropdown select Setup.
2. In the Setup window of Lightning, click on the Object Manager tab.
3. In the Object Manager search and select the Opportunity object.
4. In the setup of the Opportunity object, go to the left sidebar then click on Validation Rule.
In the window click on the New button to create a validation rule.
5. Now we will define the Validation rule formula and for that first enter the name of the validation rule in the Rule Name field.
In this example, I have named the field Probability update validation.
6. In the section Error Condition Formula, enter the formula below in the formula editor.
The condition that we applied in the above formula is as follows:
- ISCHANGED(Probability) checks if the “Probability” field is being changed. It returns true if the “Probability” field is different before and after the update.
- NOT(ISPICKVAL(StageName, ‘Prospecting’), ensures that the validation rule allows updates to the “Probability” field only when the Opportunity is in the specified stage.
- The NOT function invalidates the result of expression (ISPICKVAL(StageName, ‘Prospecting’)) making it true when the stage is NOT ‘Prospecting‘.
- The AND function combined the two conditions that we have applied in the formula. The validation rule triggers only if both conditions are true.
7. After entering the above formula click on the button Check Syntax to validate the formula.
8. In the field Error Message, enter the error message in the text area that will appear on the screen when the error condition for the validation rule is true.
At last click on the Save button.
As we click on the Save button, the validation rule will be created with the applied condition in the Opportunity object.
Now I will open an opportunity record and there I will update the probability in the Stage Name other than Prospecting.
As we can in the above image when we tried to change the Probality in the stage other than Prospecting we got an error message.
Here the NOT function made sure that if Stage is not Prospecting then probability won’t be updated and an error message will appear on the screen.
In this way, we have successfully executed the NOT function in Salesforce Lightning by creating a custom validation rule.
How to use the NOT function in Formula Field in Salesforce Classic
In this example, we will create a custom formula field for the Account object using the NOT function, where it will evaluate whether an Account is considered a “Platinum” account based on conditions when the Anual Revenue is more than 1 million and the Industry of the account must be set to Technology.
To create a custom formula field in Salesforce Classic, follow the steps below.
1. On the Home page of Salesforce Classic, click on the Setup tab that is on the upper left side of the screen.
2. On the Setup page go to the Build section on the left sidebar and click on the Customize dropdown.
In the Customize dropdown, click on Account object then select the option Fields.
3. In the Fields setup of the Account object, scroll down to the section Custom Fields and Relationships then click the New button.
4. In this window, select the Field data type as Formula and click on the Next button.
5. Now we will define the output of the formula field in the following way.
- Enter the name for the output field in Field Label. In this example, I have entered the label as Is Platinum Account.
- The Field Name will be auto-filled according to the entered Field Label.
- Select the field output type as Checkbox then click on the Next button.
6. In this step, enter the below formula in the formula editor.
NOT((AnnualRevenue < 1000000) && ISPICKVAL(Industry, "Technology"))
The condition that we have applied in the above formula is as follows.
- The condition (AnnualRevenue < 1000000) will be true if the Annual Revenue of the Account is not less than 1 million.
- The second condition ISPICKVAL(Industry, “Technology”) will be true if the Industry of the Account is set to “Technology”.
- The && operator will combine the above conditions.
- The NOT function will nullify the entire expression, so the formula will evaluate to true for Accounts that do not meet both conditions.
7. After entering the formula, click on the button Check Syntax to validate the formula and check errors then click on the Next button.
8. In this window, select the profiles that can have access to this field. To select all profiles activate the Visible checkbox and click on the Next button.
9. At last, activate the page layouts to make this field visible on the pages then click on the Save button.
As we click on the Save button, the formula field will be created in the Account object. Now to check the output of the formula field I will open an Account record.
We can see in the above image that when the amount is more than 1 million and the Industry is Technology the Platinum Status of the account is marked as checked according to the condition applied in the formula field.
Now we have successfully created a custom formula field using the NOT function in Salesforce Lightning.
In this Salesforce tutorial, we have learned about the Salesforce NOT function and discussed the use cases of the NOT function in Salesforce where we created a validation rule in Salesforce Lightning and a custom formula field in Salesforce Classic using the NOT function.
Along with this, we also learned about the syntax of the NOT function and considerations that we should be aware of while using the NOT function in Salesforce.
You may like to read:
- OR Function in Salesforce
- Salesforce ISOYEAR Function
- Salesforce FormatDuration Function
- Salesforce HOUR() Function
- CASE Function in Salesforce
- Salesforce ISOWEEK Function
I am Bijay Kumar, the founder of SalesforceFAQs.com. Having over 10 years of experience working in salesforce technologies for clients across the world (Canada, Australia, United States, United Kingdom, New Zealand, etc.). I am a certified salesforce administrator and expert with experience in developing salesforce applications and projects. My goal is to make it easy for people to learn and use salesforce technologies by providing simple and easy-to-understand solutions. Check out the complete profile on About us.