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
PraveenchanduPraveenchandu 

Use where condition in SOSL for list of ids

Example select id from account where id iN:accList 
Best Answer chosen by Praveenchandu
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi praveen,
list<list<sobject>> acc = new list<list<sobject>>();
acc = [FIND 'test' RETURNING Account(id,name WHERE id in :ids),contact(id,name WHERE id in :ids)];
list<id> ids1 = new list<id>();
for(sobject a : acc[0]){
       ids1.add(a.id); 
    }
for(sobject c:acc[1]){
    ids1.add(c.id);
}
system.debug(ids1);
//you can use this way to get ids in seperate lists
Account [] accounts = ((List<Account>)acc[0]);
Contact [] contacts = ((List<Contact>)acc[1]);
system.debug(accounts);
system.debug(contacts);

https://salesforce.stackexchange.com/questions/7334/why-does-sosl-return-a-list-of-a-list-of-sobjects-in-apex/7336

Hope this helps you
If this helps kindly mark it as solved so that it may help others in future.

Thanks and Regards

All Answers

Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi praveen,
You can write SOSL like this to serach for records in list of ids.
[FIND 'test' RETURNING Account(id,name WHERE id in :ids)];
Please refer below links which might help you further
https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_sosl_where.htm
https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_sosl_syntax.htm

Hope this helps you
If this helps kindly mark it as solved so that it may help others in future.

Thanks and Regards
PraveenchanduPraveenchandu
Hi Devi Chandrika,
Thanks for ur help 
but what I required is List of Id's from 2-3 objects using where clause in sosl and the values returned by these query should be added to the list and I should Query using soql ["select id from "any one object" where id in:"in the above list""]. 


Thanks And Regards,
Praveen Kumar
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Try this one
[FIND 'test' RETURNING Account(id,name WHERE id in :ids),contact(id,name WHERE id in :ids)];

 
PraveenchanduPraveenchandu
Hi,
Thank you For reply the records returned by the above query should be added to one list.
PraveenchanduPraveenchandu
Thanks in advance.
 
Ajay K DubediAjay K Dubedi
Hi Praveenchandu,

I have gone through your question. Please try the query below - 
 
FIND {your_id} IN ID FIELDS RETURNING your_object(Id WHERE Id In :your_List)

For more information, go to the link below -

https://salesforce.stackexchange.com/questions/157565/using-sosl-with-where-clause

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
PraveenchanduPraveenchandu
Hi, Ajay Dubedi
Thank you For reply the Id's returned by the below query should be added to the list and then I should perform soql query on the list i.e the id is present the list or not.

[FIND 'praveen' RETURNING Account(id WHERE IsImportant__c=true),contact(id WHERE IsPrimary__c=true)];

Thank You.


 
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi praveen,
list<list<sobject>> acc = new list<list<sobject>>();
acc = [FIND 'test' RETURNING Account(id,name WHERE id in :ids),contact(id,name WHERE id in :ids)];
list<id> ids1 = new list<id>();
for(sobject a : acc[0]){
       ids1.add(a.id); 
    }
for(sobject c:acc[1]){
    ids1.add(c.id);
}
system.debug(ids1);
//you can use this way to get ids in seperate lists
Account [] accounts = ((List<Account>)acc[0]);
Contact [] contacts = ((List<Contact>)acc[1]);
system.debug(accounts);
system.debug(contacts);

https://salesforce.stackexchange.com/questions/7334/why-does-sosl-return-a-list-of-a-list-of-sobjects-in-apex/7336

Hope this helps you
If this helps kindly mark it as solved so that it may help others in future.

Thanks and Regards
This was selected as the best answer
Ajay K DubediAjay K Dubedi
Hi Praveenchandu,

Please try this - 

//First get the list of lists returned by SOSL.
 
List<List<sObject>> searchList = [FIND 'praveen' RETURNING Account(id WHERE IsImportant__c=true),contact(id WHERE IsPrimary__c=true)];;

//Then separate all the lists for different objects.
 
Account[] searchAccounts = (Account[])searchList[0]; 
Contact[] searchContacts = (Contact[])searchList[1];

//Then create a List of Ids and add Ids in it from all the lists.
 
List<Id> idList = new List<Id>():

for (Account a : searchAccounts) {
    idList.add(a.Id);
}


for (Contact c : searchContacts) {
    idList.add(c.Id);
}

//Then you can use this list of Ids for SOQL

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com