3

This is my soql query,i'm getting in one soql information about account and related opportunities:

 list<account> listaccounts=  select id,name,(select id from Opportunities where SLA_Expiry__c>today AND StageName<>'cancelled' ) from Account

And this is what i get with the soql query:

enter image description here

How can i check, in the returned list of accounts, the columns with the information about the related opportunities.

I would like access the filtered opportunity part of the returned list (listaccounts) That is like this:

for(Account a: listaccounts ){
  if(There is at least one opportunity in the columns Opportunities(picture yellow colors) ){ 
      LISTA.add(a.id);
  } 

Thanks in advantage for any advice.

6

You can access related records through the relationship fields, here is a good overview in the docs.

In this case for the Account to Opportunity relationship, it is called 'Opportuntiies', but in custom fields case it can be referred to by its name suffixed by __r. In future find the lookup field that relates the child to its parent and view its details, observing the 'Child Relationship Name' field.

enter image description here

This is the code you need for your example.

list<account> listaccounts=  
    [select id,name,
        (select id from Opportunities 
            where SLA_Expiry__c>today AND StageName<>'cancelled' ) 
        from Account];
for(Account account : listaccounts)
{
    list<Opportunity> opps = account.Opportunities;
    System.debug('Size of opps ' + opps.size());
}
|improve this answer|||||
6
for(Account a: listaccounts ){

 If((a.opportunities).size()>0)
     setAccountIds.add(a.id);
}

This will indicate whether atleast one opportunity is there for an account or not .

|improve this answer|||||

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.