function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
wdwd 

What is wrong with this simple SOSL statement

I am sure that I have an Account record that the name is 'Acme', but the follow statements cannot fetch the record. 

The records.size() is 0. What is wrong?

    public static void testSOSL () {
        List<List<sObject>> results = [FIND 'Acme' IN NAME FIELDS RETURNING Account (id, name), Contact, Lead];
        
        sObject[] records = ((List<sObject>)results[0]);

        System.debug('\n\n\n' + records.size() + '\n\n\n');   
    }

 

Environment: Sandbox full

Best Answer chosen by Admin (Salesforce Developers) 
sravanthi_84sravanthi_84

The below code works fine.  Just make sure you have an account name Test alredy created.

 

***If you wish to test the result, copy paste the lines highlighted in red in the System Log and See the result ***

 

 

public class SOSLClass{
  public void fun(){
           List<List<sObject>> results = [FIND 'Test' IN NAME FIELDS RETURNING Account (id, name), Contact, Lead]; 
           sObject[] records = ((List<sObject>) results[0]); 
          System.debug('\n\n\n'+records.size()+'\n\n\n');
  }
 public static testmethod void testSOSLClass(){
         SOSLClass so = new SOSLCLass();
        Account a = new Account();
       a.Name='Test';
       insert a;
       so.fun();
 }
}

 

It returned the following result when tested in the System Log

 

19.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;VALIDATION,INFO;WORKFLOW,INFO
Execute Anonymous: List<List<sObject>> results = [FIND 'Test' IN NAME FIELDS RETURNING Account (id, name), Contact, Lead];
Execute Anonymous: sObject[] records = ((List<sObject>) results[0]);
Execute Anonymous: System.debug('\n\n\n'+records.size()+'\n\n\n');
16:12:31.664|EXECUTION_STARTED
16:12:31.664|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
16:12:31.664|SOSL_EXECUTE_BEGIN|[1,32]|FIND 'Test' IN NAME FIELDS RETURNING Account (id, name), Contact, Lead
16:12:31.767|SOSL_EXECUTE_END|[1,32]|Rows:1
16:12:31.767|METHOD_ENTRY|[3,11]|System.debug(String)
16:12:31.767|METHOD_ENTRY|[3,33]|LIST<SObject>.size()
16:12:31.767|METHOD_EXIT|[3,33]|LIST<SObject>.size()
16:12:31.767|USER_DEBUG|[3,11]|DEBUG|


1



16:12:31.767|METHOD_EXIT|[3,11]|System.debug(String)
16:12:31.767|CUMULATIVE_LIMIT_USAGE
16:12:31.767|LIMIT_USAGE_FOR_NS|sravusysnet|
Number of SOQL queries: 0 out of 100
Number of query rows: 0 out of 10000
Number of SOSL queries: 1 out of 20
Number of DML statements: 0 out of 100
Number of DML rows: 0 out of 10000
Number of script statements: 3 out of 200000
Maximum heap size: 0 out of 3000000
Number of callouts: 0 out of 10
Number of Email Invocations: 0 out of 10
Number of fields describes: 0 out of 10
Number of record type describes: 0 out of 10
Number of child relationships describes: 0 out of 10
Number of picklist describes: 0 out of 10
Number of future calls: 0 out of 10
Number of find similar calls: 0 out of 10
Number of System.runAs() invocations: 0 out of 20

16:12:31.767|CUMULATIVE_LIMIT_USAGE_END

16:12:31.767|CODE_UNIT_FINISHED|execute_anonymous_apex
16:12:31.767|EXECUTION_FINISHED

 

Please let me know if this resolved your issue.

All Answers

hisrinuhisrinu

I just tested the code and it is working aboslutely fine.

 

You need to change the dedbug to capture in your log files

 

System.debug('@@@@@@@@@@@@@@' + records.size());

wdwd

Can you tell me how did you test it?

 

For me, I directly test it on the sandbox full web site. I put this code in to a test class and then click the "Run Test" button.

 

It doesn't work.

wdwd

I try this on a Sandbox full Unlimited Edition. I always get 0. why?

I am sure I have 'Test' in the name fields.

 

public class AlexTestClass {

    public void fun(){    
        LIST<LIST<SObject>> results = [find 'Test' in name fields returning Account(id,name), Contact, Lead];        
        sObject[] records = ((List<Account>)results[0]);        
        System.debug('@@@@@@@@@@@@@@@@@@@@@@' + records.size());    
    }

    public static testmethod void main() {
        AlexTestClass atc = new AlexTestClass();
        atc.fun();
    }        
    
}

hisrinuhisrinu

public class AlexTestClass {

    public void fun(){    
        LIST<LIST<SObject>> results = [find 'Test' in name fields returning Account(id,name), Contact, Lead];        
        sObject[] records = ((List<Account>)results[0]);        
        System.debug('@@@@@@@@@@@@@@@@@@@@@@' + records.size());    
    }

    public static testmethod void main() {
        Account a = new Account(Name = 'Acme account');

        insert a;

        AlexTestClass atc = new AlexTestClass();
        atc.fun();
    }        
    
}

wdwd

Still don't work.

 

Is it something to do with my sandbox's setting?

sravanthi_84sravanthi_84

The below code works fine.  Just make sure you have an account name Test alredy created.

 

***If you wish to test the result, copy paste the lines highlighted in red in the System Log and See the result ***

 

 

public class SOSLClass{
  public void fun(){
           List<List<sObject>> results = [FIND 'Test' IN NAME FIELDS RETURNING Account (id, name), Contact, Lead]; 
           sObject[] records = ((List<sObject>) results[0]); 
          System.debug('\n\n\n'+records.size()+'\n\n\n');
  }
 public static testmethod void testSOSLClass(){
         SOSLClass so = new SOSLCLass();
        Account a = new Account();
       a.Name='Test';
       insert a;
       so.fun();
 }
}

 

It returned the following result when tested in the System Log

 

19.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;VALIDATION,INFO;WORKFLOW,INFO
Execute Anonymous: List<List<sObject>> results = [FIND 'Test' IN NAME FIELDS RETURNING Account (id, name), Contact, Lead];
Execute Anonymous: sObject[] records = ((List<sObject>) results[0]);
Execute Anonymous: System.debug('\n\n\n'+records.size()+'\n\n\n');
16:12:31.664|EXECUTION_STARTED
16:12:31.664|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
16:12:31.664|SOSL_EXECUTE_BEGIN|[1,32]|FIND 'Test' IN NAME FIELDS RETURNING Account (id, name), Contact, Lead
16:12:31.767|SOSL_EXECUTE_END|[1,32]|Rows:1
16:12:31.767|METHOD_ENTRY|[3,11]|System.debug(String)
16:12:31.767|METHOD_ENTRY|[3,33]|LIST<SObject>.size()
16:12:31.767|METHOD_EXIT|[3,33]|LIST<SObject>.size()
16:12:31.767|USER_DEBUG|[3,11]|DEBUG|


1



16:12:31.767|METHOD_EXIT|[3,11]|System.debug(String)
16:12:31.767|CUMULATIVE_LIMIT_USAGE
16:12:31.767|LIMIT_USAGE_FOR_NS|sravusysnet|
Number of SOQL queries: 0 out of 100
Number of query rows: 0 out of 10000
Number of SOSL queries: 1 out of 20
Number of DML statements: 0 out of 100
Number of DML rows: 0 out of 10000
Number of script statements: 3 out of 200000
Maximum heap size: 0 out of 3000000
Number of callouts: 0 out of 10
Number of Email Invocations: 0 out of 10
Number of fields describes: 0 out of 10
Number of record type describes: 0 out of 10
Number of child relationships describes: 0 out of 10
Number of picklist describes: 0 out of 10
Number of future calls: 0 out of 10
Number of find similar calls: 0 out of 10
Number of System.runAs() invocations: 0 out of 20

16:12:31.767|CUMULATIVE_LIMIT_USAGE_END

16:12:31.767|CODE_UNIT_FINISHED|execute_anonymous_apex
16:12:31.767|EXECUTION_FINISHED

 

Please let me know if this resolved your issue.

This was selected as the best answer
wdwd

Thank you very much!