Apex : SOQL relationship query on master-detail and lookup objects in APEX
I am trying to create a single query on master-detail and lookup objects
Currently, I have three separate queries, which are able to run in Apex and get the expected results. But, the code approach I have is not the standard.
Output of Query 1 is a list and this list is being iterated. Query2, Query3 are executed in the for loop of Query1 output. And, there are two more for loops to process query2,query3 output and these loops are the for loop Query1 loop. Below is the code snippet and queries
List<ALTIUS__Contracts__c> Details = [soql query1];
if (Details.size() > 0){
List<ContractDataWrapper> contracts= new List<ContractDataWrapper>();
for(ALTIUS__Contracts__c contract : Details){
ContractDataWrapper wrapper = new ContractDataWrapper();
wrapper.id = contract.Id;
wrapper.refnum = contract.ref_num_c;
wrapper.odnum = contract.od_num_c;
wrapper.company= contract.ALTIUS__Company__r.Name;
wrapper.contact= contract.Customer_Contact__r.Name;
List<ALTIUS__Sites__c> sites = [query2];
List<ALTIUS__Products__c > products= [query3];
if (sites.size() > 0){
List<Sites> sitesList = new List<Sites>();
for(ALTIUS__Sites__c site: sites){
SitesWrapper s = new SitesWrapper();
s.SiteName = site.ALTIUS__Site__r.Name;
s.LocationState = site.ALTIUS__Site__r.ALTIUS__State__c;
s.LocationCountry = site.ALTIUS__Site__r.ALTIUS__Country__c;
sitesList.add(s);
}
wrapper.Sites = sitesList;
}
if (products.size() > 0){
List<products> productsList = new List<products>();
for(ALTIUS__Product__r product : products){
ProductsWrapper p = new ProductsWrapper();
p.ProductName = product.ALTIUS__Product__r.ALTIUS__Product_Name__c;
p.ProductSerialNumber = product.ALTIUS__Product__r.ALTIUS__Serial_Number__c;
productsList.add(p);
}
wrapper.Products = productsList;
}
}
Query 1:
SELECT Id, ref_num_c, od_num_c,ALTIUS__Company__r.Name,Customer_Contact__r.Name FROM ALTIUS__Contracts__c
WHERE ALTIUS__Company__c IN :w.AccountId
ALTIUS__Contracts__c Ids from the above query will be used in next two queries which is on lookup objects
Query 2 :
SELECT ALTIUS__Site__r.Name, ALTIUS__Site__r.ALTIUS__State__c,ALTIUS__Site__r.ALTIUS__Country__c
FROM ALTIUS__Sites__c WHERE ALTIUS__Contract__c = :contract.Id
Query 3 :
SELECT ALTIUS__Product__r.ALTIUS__Product_Name__c,ALTIUS__Product__r.ALTIUS__Serial_Number__c
FROM ALTIUS__Products__c WHERE ALTIUS__Contract__c = :contract.Id
I am trying to change this
1.) By converting these three queries to one query and process the result in one for loop without any query execution in the loop
2.)Execute Query1 and get list of Ids
Set<Id> contractids = (new Map<Id,ALTIUS__Contract__c>(Details)).keySet();
use the contractids in Query2,Query3 with IN operator in the where condition and get the list of sites and products. Create a map with contractid as key and siteslist/productslist in the value and run for loop on query1 results and get the sites and products from Map. But can I create a map with contractid as key for Query2,3 outputs ?
Need advice to take right approach for this scenario
Read more here: Source link
