2

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...

6

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.

3
  • @ShobhitSaxena You're welcome! – sfdcfox Jun 20 '16 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 '19 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 '19 at 12:57
2

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'; 
List<List<SObject>>searchList=search.query(searchquery);

Similarly you can do like this:

String searchquery='Find '+param+ 'Other part of query'
1
  • 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 '19 at 7:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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