How to segregate or parse result set from Salesforce SOSL?

How to segregate or parse result set from Salesforce SOSL?

Sample Apex Code:

String searchQuery = ‘FIND {test} IN Name FIELDS RETURNING Account( Id, Name ), Contact( Id, Name )’;
List < List < sObject > > searchResult = search.query( searchQuery );
List < Account > listAccount = ( List < Account > )searchResult.get( 0 );
List < Contact > listContact = ( List < Contact > )searchResult.get( 1 );
for ( Account acc : listAccount ) {
    
    System.debug( acc );
    
}
for ( Contact con : listContact ) {
    
    System.debug( con );
    
}

Leave a Reply