Play Games

Search This Blog

Wednesday, May 9, 2018

Dynamic SOSL Example in Apex

Apex Code:
public class DynamicSOSLExample {
    public static List<List<SObject>> searchContact(String searchField) {
        List<List<SObject>> lstObjResults = new List<List<SObject>>();
        String searchQuery ='FIND \'' + searchField+ '\' IN ALL FIELDS RETURNING Contact(FirstName,LastName)';
        lstObjResults = search.query(searchQuery);
        system.debug('List of Contacts:'+lstObjResults);
        return lstObjResults;
    }
}

Output: Run the following line in Execute anonymous window.

DynamicSOSLExample.searchContact('Smith');