Tuesday, December 26, 2017

LIKE Operator In Salesforce

The LIKE operator in SOQL and SOSL is quite similar to the LIKE operator in SQL; it provides a mechanism for matching partial text strings and includes support for wildcards.

Suppose we need to delete some test records of Account object, all test records are created having account name is test. Account name may "testasish" or "accounttest", how can we get those record by Soql query ?
For this propose LIKE operator is useful.

For example 
List<Account> listOfAccounts;
listOfAccounts = [SELECT id, Name
                           FROM Account
                           WHERE Name  LIKE '%test%'];
This query matches both testasish,accounttest, and test.

Use Cases Of LIKE Operator

  • The % and _ wildcards are supported for the LIKE operator.
  • The % wildcard matches zero or more characters.
  • The _ wildcard matches exactly one character.
  • The text string in the specified value must be enclosed in single quotes.
  • The LIKE operator is supported for string fields only.
  • The LIKE operator performs a case-insensitive match, unlike the case-sensitive matching in SQL.
  • The LIKE operator in SOQL and SOSL supports escaping of special characters % or _.
  • Do not use the backslash character in a search except to escape a special character.

No comments:

Post a Comment