6

Essentially what I want to do is this:

SELECT Id, Email
FROM User
WHERE Email in ('first.last1%','first.last2%')

I am using this in a trigger and would like to come up with a query to match users based on the email prefix. Ideally, I like to avoid using a loop since I'm trying to avoid governor limits.

Anyone have a solution to this?

1
  • What are you trying to do exactly? Governor limits can be avoided by using proper techniques
    – EricSSH
    Aug 1 '14 at 16:25
7

what it looks like you're trying to do is a IN and LIKE. If you drop the wildcarding, your syntax is fine for IN. However, if you do need to do the Like you'll have to change approach.

If you are able to use bind variables, you can do it like this:

Set<String> emails = new Set<String>{'first.last1%','first.last2%'};
List<User> users = [SELECT Id, Email
FROM User
WHERE Email like :emails];
4
  • 2
    Thanks! Worked perfectly. Didn't realize you could pass a Set of strings to the LIKE operator. Aug 1 '14 at 16:25
  • Me neither until I tried it the other day, must parse it out to OR's I guess Aug 1 '14 at 16:26
  • I was actually writing up an answer where it took a set and looped through and constructed a soql string using OR's when I saw this answer. Good to know, was unaware of this functionality. thanks. Aug 1 '14 at 16:29
  • 1
    Yeah, I was surprised when I found it worked - I wasn't expecting it to Aug 1 '14 at 16:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.