2

I write a sample query to fetch account Ids from cases of a particular record type.Then i wrote another sample query parent to child relationship to fetch opportunity ids from accounts.

My sample code

List<Case> c=new List<Case>();
c=[Select Id,accountId from case where RecordTypeId = '01290000000sF3L'];
List<Id> accountIds= new List<Id>();
List<Account> accounts=new List<Account>();
List<Opportunity> opps=new List<Opportunity>();
for(Case cl:c) 
{
accountIds.add(cl.accountId);
}
accounts=[Select Id,name,(select Id,name from opportunities) from account       
where Id in:accountIds];
System.debug('Data in accounts '+accounts);

But my issue is how to get opportunity id from above Soql accounts means how to add those opportunity id's to the list.

for(Account ao:accounts) {
//How to fetch Opportunity ids from above account list.
}
4

Most efficient:

Set<Id> childIds = new Map<Id, Opportunity>(ao.Opportunities).keySet();

You can also loop through them:

for (Opportunity child : ao.Opportunities)
{
    // ...
}
2

One quick addition to Himanshu's post - add [] to accounts in the query. This will make the query to fetch 200 records per iteration than 1 record per iteration. You can find this at Trailhead data modelling.

However I would suggest not to put SOQL in a for loop. Query it outside as in your code.

for(Account[] ao:[Select Id,name,(select Id,name from opportunities) from account       
where Id in:accountIds]) {
   for(Opportunity opp : ao.opportunities)
   {
       //Other Operation you want to perform with every opportunity
       System.debug(opp.Id);
   }
}
1

You can access relationship data using relationship name so your code can be modified in following way

List<Case> c=new List<Case>();
c=[Select Id,accountId from case where RecordTypeId = '01290000000sF3L'];
List<Id> accountIds= new List<Id>();
List<Opportunity> opps=new List<Opportunity>();
for(Case cl:c) 
{
accountIds.add(cl.accountId);
}

System.debug('Data in accounts '+accounts);

for(Account ao:[Select Id,name,(select Id,name from opportunities) from account       
where Id in:accountIds]) {
   for(Opportunity opp : ao.opportunities)
   {
       //Other Operation you want to perform with every opportunity
       System.debug(opp.Id);
   }
}
1

Just to add in your code:

for(Account ao:accounts) {
    opps.add(ao.opportunities);
}
0

Not sure how big your org is, but this should get what you're looking for. I would run the actual query part here in workbench first to see how intensive it is and maybe put a filter on the account query.

Set<Id> opptIds = new Map<Id,Opportunity>([
    SELECT Id 
    FROM   Opportunity 
    WHERE  AccountId IN (SELECT Id 
                         FROM   Account) 
        AND AccountId IN (SELECT AccountId 
                          FROM   Case 
                          WHERE  RecordTypeId = '01290000000sF3L') 
]).keyset(); 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.