1

I have fixed set of Account Names stored in one custom object.

Set<String> NameSet = new Set<String>();

Custom object NameSet account Name values: Intel, HP, Dell, IBM etc

I have a query on Account object. My Account Names are Intel-Aus, HP, Dell-US, IBM etc

List<Account> accList = [Select Name from Account where Name IN: NameSet]

The above accList only gives 2 records HP and IBM!

Intel and Dell do not come in the list because these account names have "hyphen" in it.

How do I write the query to get all the Account Names using %Like% in a single query.

0

1 Answer 1

2

You can do like this way

Create set of string %Name1%, %Name2%etc

List<String> lstString = new List<String>{`%Name1%`, `%Name2%`, `%Name3%`};
List<Account> accList = [Select Name from Account where Name LIKE: lstString ];

Updates

List<String> lstString = new List<String>{'%Intel%', '%HP%', '%Dell%' , '%IBM%'};

for(Account obj : [Select Name from Account where Name LIKE: lstString ]){
    system.debug(obj);
}

This will return all the Accounts with name Intel-Aus, HP, Dell-US, IBM.

enter image description here

7
  • but my problem is the other way. Name1-123 LIKE %Name1%. Will your query work for this too?
    – sfdcFanBoy
    Feb 29, 2016 at 14:55
  • @sfdcFanBoy what do you want ?
    – Ratan Paul
    Feb 29, 2016 at 15:21
  • I meant are these 2 same: (%Name1-123% LIKE Name1) and (Name1-123 LIKE %Name1%) ?
    – sfdcFanBoy
    Feb 29, 2016 at 23:29
  • Exactly. So, if you see my Account Names are Name1-123, Name3-xyz (with hyphens) where as the Set contains just Name1, Name3. So, will the LIKE get the results?
    – sfdcFanBoy
    Mar 1, 2016 at 8:28
  • Let us continue this discussion in chat.
    – Ratan Paul
    Mar 1, 2016 at 8:47

Not the answer you're looking for? Browse other questions tagged .