Can LIKE Be Used with a List in SOQL

Can LIKE Be Used with a List in SOQL?

Spread the love

Question

Is there a way to use the LIKE operator with a list of values in SOQL, similar to how we use IN? For example, can we write a query like this:

SELECT Id FROM Account WHERE Name LIKE IN :nameList

Where nameList is a list of strings? Instead of dynamically constructing a query with multiple OR conditions, is there a more elegant way to achieve this?

Answer

No, SOQL does not support combining LIKE with IN directly. However, there is a simple and efficient alternative that achieves the same result without manually constructing an OR condition for each value. Instead of using IN, you can pass a list of wildcard-matching strings to LIKE in SOQL, as shown in the following example:

String[] filters = new String[]{'acme%', 'ib%'};
List<Account> accs = [SELECT Id, Name FROM Account WHERE Name LIKE :filters];

Code Explanation:

String[] filters = new String[]{'acme%', 'ib%'};

This line defines a list of string values called filters.Each value in the list is a string containing a wildcard (%).'acme%' means “match any string that starts with ‘acme’.”'ib%' means “match any string that starts with ‘ib’.”

List<Account> accs = [SELECT Id, Name FROM Account WHERE Name LIKE :filters];

This SOQL query retrieves all Account records where the Name field matches any of the patterns specified in the filters list.The LIKE operator allows partial matching using wildcards (%).By passing filters as a bind variable (:filters), SOQL automatically expands it into multiple LIKE conditions, making it equivalent to:

SELECT Id, Name FROM Account WHERE Name LIKE 'acme%' OR Name LIKE 'ib%'

This means that if an account’s name starts with either “acme” or “ib”, it will be included in the results. The % wildcard ensures that any characters after these prefixes are matched.

This method is much cleaner than manually concatenating an OR condition for each value in nameList. If you were to construct the query dynamically, it would require looping through the list and appending LIKE conditions with OR, which can lead to messy and error-prone code. Instead, using a list with LIKE ensures that SOQL handles the filtering efficiently while keeping the query readable and maintainable.

In summary, while SOQL does not support LIKE IN, the ability to pass a list of wildcard patterns to LIKE provides a clean and effective solution to achieve the same result.

How It Works:

If there are accounts named “Acme Corp”, “Acme Solutions”, or “IBM Technologies”, they will be included in the results.

This approach eliminates the need for manually constructing an OR condition, making the code cleaner and more efficient.

It also improves security by avoiding string concatenation, which can lead to SOQL injection risks.

Key Takeaways:

Wildcard Usage (%): Used to match any sequence of characters after the specified prefix.

List Binding in SOQL: Instead of dynamically constructing OR conditions, Salesforce automatically expands the list into multiple LIKE conditions.

Efficiency & Maintainability: Using a list makes the query more readable and easier to maintain, especially when dealing with multiple filter values.

Kick Start Your Career with Real-Time Project-Based Salesforce Training

Our Salesforce course is designed to give you a comprehensive understanding of the Salesforce platform, equipping you with the essential skills needed to excel in the CRM industry. The curriculum covers key modules such as Salesforce Admin, Developer, and AI, combining theoretical learning with hands-on experience. Through real-world projects and practical exercises, you will develop the expertise to solve complex business challenges using Salesforce solutions. Our experienced instructors ensure you gain both technical proficiency and industry-relevant insights to succeed in the Salesforce ecosystem.

In addition to technical training, our Salesforce training in Hyderabad offers personalized mentoring, certification support, and interview coaching to enhance your career prospects. You’ll have access to extensive study materials, hands-on project work, and continuous guidance throughout your learning journey. By the end of the program, you’ll be fully prepared for certification exams and equipped with the problem-solving skills that employers value. Take the first step toward a successful Salesforce career and unlock new opportunities. Sign up for a free demo today!

Open Chat
1
Dear Sir/Madam
How can I help you?