Reply
Trusted Contributor
Big Ears
Posts: 160
0
Accepted Solution

Use of LIKE and Wildcards in SOQL queries

I'm trying to produce a list of leads that share the same email address root (i.e. the part after the @) symbol and I think I'm not correctly assembling my SOQL query.

public List<Lead> getleadz(){ Lead ld = [Select id, email FROM Lead where id = :lead.id]; String EmailSubstring = ld.email.substring(ld.email.indexOf('@',0)); leadz = [Select id, Name, company, status, email, phone from Lead where email like :EmailSubstring];

return leadz; }

 

Unfortunately, the SOQL query that defines "leadz" returns 0 lines, even though I know that there are other leads with similar email addresses. I'm probably missing a wildcard, but whenever I put a wildcard next to ":EmailSubstring" in my SOQL query, I get a save error as my syntax is all wrong.

 

Does anybody know where I'm going wrong?

 

With thanks,

Andy

 

 

Moderator
SimonF
Posts: 7,984
0

Re: Use of LIKE and Wildcards in SOQL queries

You can't mix'n'match literals and binding for a given position in the query, so you'll need to add the wildcard to the actual value of the EmailSubstring variable, e.g.

 

EmailSubstring = '%' + EmailSubstring; 

Cheers
Simon
docs | blog | twitter
Trusted Contributor
Big Ears
Posts: 160
0

Re: Use of LIKE and Wildcards in SOQL queries

Simon,

 

You're something like a genius, thanks!

 

Andy