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
8hrWait8hrWait 

IDs in IN clause not giving results

Hi,

 

Can anyone please let me know, how to retrieve database records using IDs in 'IN' clause for a query on a custom object.

 

The below query
select Id, Name from <customObject>__c gives all records


But, if a where clause is added to filter by Ids, no results are displayed.
For example,
select Id, Name from <customObject>__c where ID in (Set<String>) -> gives no results

 

Thx

Best Answer chosen by Admin (Salesforce Developers) 
Mayank_JoshiMayank_Joshi

Take this as :

 

 

Use Set as below :

Set<Id> test = New Set<Id> ();

List<customObject__c> Test2 = [select Id, Name from customObject__c where ID IN:test ];

All Answers

Mayank_JoshiMayank_Joshi

Take this as :

 

 

Use Set as below :

Set<Id> test = New Set<Id> ();

List<customObject__c> Test2 = [select Id, Name from customObject__c where ID IN:test ];

This was selected as the best answer
SFDC_LearnerSFDC_Learner

Use like this:

 

set<Id> setIds = new set<Id>();
for(Account objA :[select id,name from Account]){
setIds.add(objA.id);
}

List<Account> lstA = [select id,name from Account where ID IN : setIds];

8hrWait8hrWait

Thank you for the solution.
I was using a custom query manager to exceute the query.For some reason, the code was not identifying the binding variable.


A direct call of Database.execute(query) worked.