+ Start a Discussion
Timothy SmithTimothy Smith 

Access CustomObject sibling to Case inside Case Trigger

I am writing an after-insert trigger on the Case object.  I am looking to send an email to one of two people depending on a field that is a child of the Account object.  A sibling to Case.  In my journey I have found that it is not possible to query a sibling (correct me if I'm wrong).  So I've decided,  once I have the Case, I will take the AccountId and do all of my logic and querys from there.

CustObject_Chld2Acct.fieldname - Child to Account, sibling to Contact
 
List<AggregateResult> AggregateResultList = [SELECT AccountId, Account.Name name, COUNT(Id) co, 
                                    FROM Case
                                    WHERE CreatedDate = LAST_N_DAYS:5
                                    GROUP BY AccountId, Account.Name
                                    HAVING COUNT(Id)  >= 8];
                       
        Set<Id> accIdSet = new Set<Id>();
        for(Case c: AggregateResultList) {
        if(c.AccountId != null) {
           System.debug(c.AccountId);
           // Now i have the Account ID, what do I do?  
           //Need to access fields in Account Object (Parent)
           //Need to access fields in CustObject_Chld2Acct          
        }

 

Best Answer chosen by Timothy Smith
Abdul KhatriAbdul Khatri
Please take the following approach, it will help you
Set<Id> idAccountSet = new Set<Id>();
for(Case case : Trigger.new){
	idAccountSet.add(case.AccountId);   
}

List<Account> accountList = [SELECT Id, (SELECT Id FROM ChildObjs) FROM Account WHERE Id = :idAccountSet];
for(Account account : accountList)
{
     if(account.ChildObjs != null) {
           
          for(ChildObj childObjRec : account.ChildObjs)
          {
                  //Code here for collectiong email
          }
     {
}