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
NyshaaNyshaa 

How to fetch Id from the first query and fetch the record for the 2nd query.

Query 1:
       List<Service_Line__c> ServiceLineList =[Select id, Name  from Service_Line__c ];

Query 2:
         List<Service_Line_Item__c > ServiceLineItemList =[Select Name,Type__c,Component_Serviced__c,Effort_Required_Man_Hours__c, Cost_Incurred__c from Service_Line_Item__c ];

I want to use the fetched id from the 1st query and display the result of the 2nd query .
The 2nd query should only be performed on the result fetched from the 1st query 
How can I pass id of 1st query and get the results of the 2nd query 
Thanks in Advance!
2nd query is dependent on the 1st query
Best Answer chosen by Nyshaa
Agustin BAgustin B
Hi Nyshaa, use a set to save all the ids, and on the query 2 you use the IN operator that will only take those ids.
List<Service_Line__c> ServiceLineList =[Select id, Name  from Service_Line__c ];
Set<Id> setToQuery = new Set<Id>();
for(Service_Line__c sl:ServiceLineList ){
setToQuery.add(sl.Id);
}
List<Service_Line_Item__c > ServiceLineItemList =[Select Name,Type__c,Component_Serviced__c,Effort_Required_Man_Hours__c, Cost_Incurred__c from Service_Line_Item__c WHERE Service_Line__r.Id IN : setToQuery];
I assume on the query 2 that Service_Line__r.Id  is what you need to check.

If it helps please mark as correct, it may help others.