1

New dev here and I am trying to make a SOSL query to pull accounts that contain a string variable. Not returning any results. Any guidance is appreciated

Here's the vfp

<apex:page controller="SoslTestClass">
    <apex:pageBlock>
        <apex:pageBlockTable value="{!accList}" var="acc">
            <apex:column >
                <apex:facet name="header">Account Name</apex:facet>
                <apex:outputText value="{!acc.Name}"/>
            </apex:column>
            <apex:column >
                <apex:facet name="header">Website</apex:facet>
                <apex:outputText value="{!acc.Website}"/>
            </apex:column>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

And the Apex code

public with sharing class SoslTestClass {
    Public List<account> accList{get;set;}
    Public String webName = '.com';

    Public void soslSearch(){
        accList = New List<account>();
        List<List<sObject>> searchList = [FIND :webName IN ALL FIELDS RETURNING Account(Name, Website)];
        accList = ((List<account>)searchList[0]);
        return;
    }
}
2
  • Seems fine to me. Are you sure you actually have any matching data? – Adrian Larson Sep 14 '16 at 23:08
  • Yes, I have multiple accounts that contain '.com' in their website (string field) – asdkhalifa Sep 14 '16 at 23:13
2

I ran your code exactly as you have it and it runs just fine.

If you are running it from your page, I do not see an action to perform the search so how are you executing the search?

Per you comment:

To Execute your code on page load:

  1. In the constructor, call the soslSearch() method

OR

  1. Add an action parameter to the page tag and set it to {!soslSearch}

If you are running this during a test methods or the data you think you have is not present.

SOSL executed from test methods needs to have the resultant IDs hardcoded during the test:

To ensure that test methods always behave in a predictable way, any Salesforce Object Search Language (SOSL) query that is added to an Apex test method returns an empty set of search results when the test method executes. If you do not want the query to return an empty list of results, you can use the Test.setFixedSearchResults system method to define a list of record IDs that are returned by the search

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_SOSL.htm

example output from your code

Executing:
======================================================================================================== 
 Public List<account> accList{get;set;} 
 String webName = '.com'; accList = New      
 List<account>(); List<List<sObject>> searchList = [FIND :webName IN ALL FIELDS RETURNING Account(Name, Website) Limit 1]; 
 accList =((List<account>)searchList[0]);

system.debug(accList); 

======================================================================================================== Success.

19:25:59.19 (51817077)|USER_DEBUG|[7]|DEBUG|(Account:{Name=xxxxxx.com / xxxxxxx.com B2C, Id=001a00000xxxxxxxx})

3
  • Ah yes, they could just change void soslSearch to List<Account> getAccList. – Adrian Larson Sep 14 '16 at 23:34
  • I would like to execute the search on page load – asdkhalifa Sep 14 '16 at 23:43
  • @asdkhalifa - See updated answer – Eric Sep 14 '16 at 23:50

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.