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
Rich FiekowskyRich Fiekowsky 

How to find all Parent records which have one sort of child record and also another sort of child record?

Users need to identify all the Accounts which have one or more Opportunity children for which Status='Won'  and also have one or more Opportunity children for which Status='Lost' . A report would be ideal but any easy solution would be nice. Of course I can write batchable Apex to traverse all the data but there must be a simpler way.  
SUCHARITA MONDALSUCHARITA MONDAL
Hi Rich,

You can try something as below.

List<Opportunity> oppList = new List<Opportunity>([SELECT Id, Status, AccountId  FROM Opportunity
                                                                                  WHERE  Status IN  ('End-Won','End-Lost')]);
                                                   
    Set<Id> lostAccId = new Set<Id>();  // Using SET collection for unique Ids /Non-Duplicate Ids
    Set<Id> wonAccId = new Set<Id>();

// Iterating list and setting all AccountId
    for(Opportunity opp : oppList){
        if(opp.status == 'End-Lost'){
            lostAccId.add(opp.AccountId);
        }
        if(opp.status == 'End-Won'){
            wonAccId.add(opp.AccountId);
        }
    }

Hope this helps!

Thanks,
Sucharita