I wanna search for a particular string obtained by my function parameter in Contact & Lead FirstName & LastName fields.. The difficulty is how to compose a SOQL statement for that,as I am getting problem with the following piece of code: enter image description here

However when I pass a string,it works perfect,it is not just allowing the parameter obtained to be passed in the query..

Code that works

Am I doing something wrong? Please let me know...

2 Answers 2


I'm pretty sure there's a duplicate out there somewhere, but the answer is you have to bind to the query:

List<SObject[]> searchList = [FIND :param IN ALL FIELDS RETURNING Contact(FirstName, LastName), Lead(FirstName, LastName)];

Do not use the quote (') or the ({}) characters. You can bind directly to a variable.

  • @ShobhitSaxena You're welcome!
    – sfdcfox
    Jun 20, 2016 at 6:01
  • what if I want to make "Contact, Lead(FirstName, LastName)" this dynamic. I'm trying to create list of string, but it is not working
    – Nehal
    Jun 10, 2019 at 7:13
  • @Nehal You can't bind field/object names. You would use dynamic SOSL, as mentioned in the other answer. Did you check the documentation? You might want to ask a new question if you're stuck. We'll be glad to help you out.
    – sfdcfox
    Jun 10, 2019 at 12:57

I am not very familiar with SOSL but i think It can be done using dynamic SOSL.

for eg.

String searchquery='FIND\'Edge*\'IN ALL FIELDS RETURNING Account(id,name),Contact, Lead'; 

Similarly you can do like this:

String searchquery='Find '+param+ 'Other part of query'
  • what if I want to make other part of query "Contact, Lead" also dynamic. I'm trying to create list of string, but it is not working
    – Nehal
    Jun 10, 2019 at 7:14

You must log in to answer this question.

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