+ Start a Discussion
bohemianguy100bohemianguy100 

SOSL query contacts and accounts

Is thee a way to use sosl to query contact records but include the account field in the Find IN ALL Fields?

 

For example, I'm searching by first name, last name and I also want to include account name in the search.  So, if I type in 'Acme', it will find all contact records that are related to the 'Acme' account.

 

Here is the method I'm currently using:

 

    public PageReference contactSearch() {
    	system.debug('***************************** Company' + company);
    	query = firstname + ' ' + lastname + ' ' + company;
		try {
			List<List<Contact>> searchResults = [FIND :query IN ALL FIELDS RETURNING Contact (id, firstname, lastname, Name, account.Name, Personal_Detail_Comments__c, account.BillingStreet, account.BillingCity, account.BillingState, account.BillingPostalCode), Account];	
			resultSetSize = searchResults[0].size();
			for(Contact c : searchResults[0]) {
				contactLinesForPage.add(new ContactWrapper(contactLinesForPage.size(), c, ''));
			}   
		}
		catch(Exception ex) {
			resultSetSize = null;
			ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, ex.getMessage());
            ApexPages.addMessage(msg);
		}
		return null; 	
    }

 

 

Thanks for any help.

ezdhanhussainezdhanhussain

you can use nested soql like this

[Select Id, Name, (Select Id, First Name,Last name From Contacts) From Account where name=:'acme'];

bohemianguy100bohemianguy100

Thanks for your reply.  The rested soql would return a list of accounts.  I'm looking to return a list of contacts but allow the search to include the account name search within the find IN ALL FIELDS using SOSL.

 

Is it not possible to use SOSL and return a list of contacts?