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
Manish Anand 12Manish Anand 12 

Error- Unexpected Token: 'List'

Hi,
I am trying to execute below SOSL query in Apexcode:

List<List<Sobject>> searchlist=[FIND 'Pencil*' IN ALL FIELDS RETURNING Merchandise__c (Id,Name), Invoice_Statement__c];
Merchandise__c[] merList = ((List<Merchandise__c>) searchList[0]);
Invoice_Statement__c[] invList = ((List<Invoice_Statement__c) searchList[1]);
System.debug('Found' + merList.size() + 'merchandise items.'); 
System.debug('Found' + invList.size() + 'invoice statements.');

I get an error message- Unexpected Token: 'List'
What could be the reason?
 
Temoc MunozTemoc Munoz
Invoice_Statement__c[] invList = ((List<Invoice_Statement__c>) searchList[1]);

You're missing a closing '>'
Mahesh DMahesh D
Hi

You can remove the type casting of Lists.

See the below code:

public class SOSLExample {
    public void findRecords() {
        //List<List<SObject>> listObj = [FIND 'Test' IN ALL FIELDS returning Account, Contact, Case, Lead];
        List<List<SObject>> listObj = [FIND 'Test' IN ALL FIELDS returning Account(Id, Name, AccountNumber), Contact, Case, Lead];
        
        System.debug('===================listObj:'+listObj);
        for(List<SObject> lObj: listObj) {
            System.debug('---------------lObj:'+lObj);
        }
        
        List<Account> accList = listObj.get(0);
        List<Contact> conList = listObj.get(1);
        List<Case> caseList = listObj.get(2);
        List<Lead> leList = listObj.get(3);
        
        System.debug('********************AccList:'+accList);
        
        for(Account acc: accList) {
            System.debug('------------------Account acc:'+acc);
            System.debug('------------------acc.Id:'+acc.Id);
            System.debug('------------------acc.Name:'+acc.Name);
        }
    }
}

Which I have written in my developer sandbox and it works well.

Regards,
Mahesh
Manish Anand 12Manish Anand 12
I see. My Bad. Thanks Temoc.

@Mahesh:- I copy-pasted your code in my developer sandbox, looks fine to me, but when I execute it it gives following error.
No such column 'AccountNumber' on entity 'Account'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names.
I am new to salesforce.Not sure, what I am missing here.
Mahesh DMahesh D
public class SOSLExample {
    public void findRecords() {
        //List<List<SObject>> listObj = [FIND 'Test' IN ALL FIELDS returning Account, Contact, Case, Lead];
        List<List<SObject>> listObj = [FIND 'Test' IN ALL FIELDS returning Account(Id, Name), Contact, Case, Lead];
        
        System.debug('===================listObj:'+listObj);
        for(List<SObject> lObj: listObj) {
            System.debug('---------------lObj:'+lObj);
        }
        
        List<Account> accList = listObj.get(0);
        List<Contact> conList = listObj.get(1);
        List<Case> caseList = listObj.get(2);
        List<Lead> leList = listObj.get(3);
        
        System.debug('********************AccList:'+accList);
        
        for(Account acc: accList) {
            System.debug('------------------Account acc:'+acc);
            System.debug('------------------acc.Id:'+acc.Id);
            System.debug('------------------acc.Name:'+acc.Name);
        }
    }
}

Try this and check the results.

Regards,
Mahesh
Manish Anand 12Manish Anand 12
Ok. It compiles successfully. But doesn't show any output.

Regards,
Manish
Mahesh DMahesh D
Hi Manish,

How are you executing the method.

Go to Developer Console --> Debug --> Open Execute Anonymous Window

SOSLExample se = new SOSLExample();
se.findRecords();

select the checkbox 'Open Log' and click on the Execute button.

Regards,
Mahesh
Manish Anand 12Manish Anand 12
Hi Mahesh,

I see it working.Thanks.
But do you know the reason, why it gave an error with 'AccountNumber'? It looks fine to me.

Regards,
Manish
Mahesh DMahesh D
Any of the below reasons Manish,

--> the field is set to Hidden via field level security for the current user profile (if you're an admin fields can still be hidden) you will receive the above error.
--> If the organization is using Person Accounts and you are accessing a PersonAccount record then this field may not be available for the person account record type.

Regards,
Mahesh
Manish Anand 12Manish Anand 12
Hi Mahesh,

It worked !!. Thanks for your responses.Appreciate it.

Regards,
Manish.
Mahesh DMahesh D
Thank you Manish,

Please mark it as solved.

Regards,
Mahesh
Mahesh DMahesh D
Hi Manish,

Could you please mark it as solved.

Regards,
Mahesh
Manish Anand 10Manish Anand 10
Hi Mahesh,

I am totally new to this platform.How do I mark this as solved?
Mahesh DMahesh D
Hi Manish,

No problem, I can help you here.

Go to any of the replies and select it as a "Best Answer" and it will automatically marked as "Solved".

Regards,
Mahesh
Mahesh DMahesh D
Hi Manish,

You can change the status of this Case as well.

Regards,
Mahesh
Manish Anand 10Manish Anand 10
Hi Mahesh,

I don't see an option for "Best Answer" for  any of the above replies.
How do I mark this as resolved.

Regards,
Manish.
Mahesh DMahesh D
Hi Manish,

If you take the mouse pointer to the replies sure you will get an option for selecting the best answer.

Regards,
Mahesh