Comparison Operators in Salesforce

Introduction

Comparison operators in Salesforce are very useful to fetch the data in Salesforce Database using SOQL. They are used to compare the data with the values we provide. By using these operators we can customize the data we wanted to fetch from the database. Application of these operators at suitable positions in the queries retrieves the exact data needed. Hence let’s quickly see some of the important Operators in the below table.

 

Comparison Operators in Salesforce

Operator Name Description Example
= Equals Equals operator fetches the data only which is equal to the values provided. SELECT Name FROM Account WHERE Name = ‘Amazon’
!= Not equals Not equals operator fetches the data apart from the value provided. SELECT Name FROM Account WHERE Name != ‘Amazon’
< Less than Less than operator fetches the data whose fields value is less than the provided value. SELECT Name FROM Account WHERE NumberOfEmployees < 10
<= Less than or equal Less than or equal operator fetches the whose field value is less than or equal to the provided value. SELECT Name FROM Leave__c WHERE NoOfLeaves__c <= 5
> Greater than Greater than operator fetches the whose field value is Greater than the provided value. SELECT Name, Phone FROM Student__c WHERE Backlogs > 7
>= Greater than or  equal Greater than or equal operator fetches the whose field value is Greater than or equal to the provided value. SELECT Name FROM Leave__c WHERE NoOfLeaves__c >= 5
LIKE Like Like operator fetches the data when the data matches with the value provided.

Beyond this, we have your wildcard supported in LIKE Keyword. They are “%”  and “_”.

% matches zero or more characters.

_ matches exactly one character.

SELECT Name FROM student__c WHERE Lastname LIKE ‘vi%’

 

SELECT Name FROM student__c WHERE Lastname LIKE ‘%vi’

 

SELECT Name FROM student__c WHERE Lastname LIKE ‘_vi%’

IN IN IN operator fetches the when the data matches the given values.

We represent the values inside a parenthesis and each value in quotes.

SELECT Name FROM Contact WHERE State IN (‘Michigan’, ‘Florida’)
NOT IN NOT IN NOT IN operator fetches the when the data which doesn’t match the given values. SELECT Name FROM Contact WHERE State NOT IN (‘Michigan’, ‘Florida’)

 

Reference

https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_comparisonoperators.htm#:~:text=SOQL%20queries%20can%20include%20comparison,use%20in%20a%20SELECT%20statement.&text=String%20comparisons%20using%20the%20equals,insensitive%20for%20all%20other%20fields.

https://www.sfdc99.com/2013/10/12/comparison-operators/

 

Author

  • Barry Allen

    A Full Stack Developer with 10+ years of experience in different domain including SAP, Blockchain, AI and Web Development.

    View all posts

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.