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
bujjibujji 

Error in SOSL

Hi,

 

I am trying small query but i am getting error while try to display.

 

ERROR-- line 4, column 59: Initial term of field expression must be a concrete SObject: LIST

 

String searchTerm = 'abc';
List<List<Account>> accs =[FIND:searchTerm RETURNING Account(Id,Name)];
for(List<Account> a: accs){
system.debug('**************SOSL Result*****************'+a.Name);
}

 

I want to see the account name.

 

Thanks,

Bujji

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

Your for list iterator variable is a list of accounts, so you need to iterate those:

 

String searchTerm = 'abc';
List<List<Account>> accs =[FIND:searchTerm RETURNING Account(Id,Name)];
for(List<Account> a: accs){
for (Account cand : a) {
system.debug('**************SOSL Result*****************'+cand.Name);
} }

 

All Answers

bob_buzzardbob_buzzard

Your for list iterator variable is a list of accounts, so you need to iterate those:

 

String searchTerm = 'abc';
List<List<Account>> accs =[FIND:searchTerm RETURNING Account(Id,Name)];
for(List<Account> a: accs){
for (Account cand : a) {
system.debug('**************SOSL Result*****************'+cand.Name);
} }

 

This was selected as the best answer
harsha__charsha__c

Hi bujji,

 

Bob is exactly right.

 

The error is in the followed line

 

system.debug('**************SOSL Result*****************'+a.Name);

 

here "a" refer to List<Account> type not simply Account.

 

So a.Name is invalid and it means to (List<Account>).Name which is invalid syntax.

 

The followed will give you the first Account Name from the list.

 

system.debug('**************SOSL Result*****************'+a[0].Name);

 

You can refer Bob's Comment in order to Iterate through all the Accounts retrieved.

 

Thanks

V SubhashV Subhash

Hi All,

 

Can you please try like below.

Explanation : SOSL query will return only 'sObjects' not actual objects. So we need to convert the 'sObject' to actual object 'account' then we can use as like account. I just tryied.

 

Please let me know If I am wrong.

=============================================================

String searchTerm = 'acc';
List<List<Sobject>> accs =[FIND:searchTerm RETURNING Account(Id,Name)];
for(SObject acc : accs[0]){
Account a = (Account)acc;
system.debug('**************SOSL Result*****************'+a.Name);
}

================================================================