- SOSL is supported only in apex class and the anonymous block.
- SOSL can not be used in apex trigger.
- If SOSL does not return any records for a specified saelsforce object type, and the result for that sObject type is empty list
The following example a simple SOSL query string.
String soslString='FIND\'Edge*\'IN ALL FIELDS RETURNING Account(id,name,BillingCity),Contact, Lead'; List<List<SObject>>searchList=search.query(soslString); List<Account> accounts = (List<Account>)searchList.get(0); List<Contact> contacts = (List<Contact>)searchList.get(1); List<Lead> leads = (List<Lead>)searchList.get(2);The result lists are always returned in the same order as they were specified in the dynamic SOSL query. From the example above, the results from Account are first index(0), then Contact, then Lead.
Bind Variable in Dynamic SOSL
You can use bind variables in dynamic SOSL string as the following:
This code finds cases which are description contains CASE1 or CASE2 and Account ID is 0019000000NCTyt
String filters ='CASE1 OR CASE2';
String accountID='0019000000NCTyt';
//Account is relationship name of lookup field AccountId on Case
String sosl='FIND:filters IN ALL FIELDS RETURNING Case(id,Subject,Description WHERE Account.Id=:accountID)';
List<List<SObject>> result=search.query(sosl);
System.debug('Result:'+result);
Result:((Case:{Description=Hello, This is CASE2, AccountId=0019000000NCTytAAH, Subject=test CASE2, Id=5009000000pMe00AAC}, Case:{Description=Hello, This is CASE1, AccountId=0019000000NCTytAAH, Subject=Test CASE1, Id=5009000000pMdxBAAS}))
No comments:
Post a Comment