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
Manjunath reddy 25Manjunath reddy 25 

not able to get child records from parent in for loop

I am trying to update the field of the child object when the type field in case is changed, but iam getting empty child list
system.debug('c.Casechilds__r'+c.Casechilds__r);, can some one help me on this

debug logs is as follows
11:06:12.0 (3510151)|SOQL_EXECUTE_BEGIN|[8]|Aggregations:1|SELECT id, (SELECT id, Field_to_update__c FROM Casechilds__r) FROM case WHERE id IN :tmpVar1 11:06:12.0 (9078923)|SOQL_EXECUTE_END|[8]|Rows:1 11:06:12.0 (9255628)|USER_DEBUG|[9]|DEBUG|caseList(Case:{Id=5002800000O4w0fAAB}) 11:06:12.0 (9310150)|USER_DEBUG|[10]|DEBUG|caseIds (5002800000O4w0fAAB) 11:06:12.0 (10027977)|USER_DEBUG|[12]|DEBUG|cCase:{Id=5002800000O4w0fAAB, IsDeleted=false, CaseNumber=00001027, ContactId=null, AccountId=null, AssetId=null, BusinessHoursId=01m28000000DrvlAAC, ParentId=null, SuppliedName=null, SuppliedEmail=null, SuppliedPhone=null, SuppliedCompany=null, Type=Electrical, Status=New, Reason=null, Origin=Phone, Subject=null, Priority=Medium, Description=null, IsClosed=false, ClosedDate=null, IsEscalated=false, OwnerId=00528000002LYzBAAW, IsClosedOnCreate=false, CreatedDate=2016-08-09 05:34:43, CreatedById=00528000002LYzBAAW, LastModifiedDate=2016-08-09 05:36:12, LastModifiedById=00528000002LYzBAAW, SystemModstamp=2016-08-09 05:36:12, LastViewedDate=null, LastReferencedDate=null, EngineeringReqNumber__c=null, SLAViolation__c=null, Product__c=null, PotentialLiability__c=null} 11:06:12.0 (10168326)|USER_DEBUG|[13]|DEBUG|c.Casechilds__r() 11:06:12.10 (10443794)|CUMULATIVE_LIMIT_USAGE 11:06:12.10 (10443794)|LIMIT_USAGE_FOR_NS|(default)|
trigger updateChild on Case (after insert, after update){
    list<id> caseIds = new list<id>();
    list<id> childCaseIds = new list<id>();
    for(case c:trigger.new){
        caseIds.add(c.id);
    }
    
   list<case> caseList= [select id,(select id,Field_to_update__c from Casechilds__r)from case where id IN : caseIds ];
   system.debug('caseList'+caseList);
   system.debug('caseIds '+caseIds );
   for(case c : trigger.new){
       system.debug('c'+c);
       system.debug('c.Casechilds__r'+c.Casechilds__r);
       for(Case_child__c casechild :c.Casechilds__r){
           system.debug('casechild '+casechild );
           casechild.Field_to_update__c  = c.type;
           system.debug('casechild.Field_to_update__c'+casechild.Field_to_update__c);
           childCaseIds.add(casechild.id);
           system.debug('childCaseIds'+childCaseIds);
       }
      }
   // update childCaseIds;

}

 
BALAJI CHBALAJI CH
Hi Manjunath,

In your second for loop at line number 11, instead of looping Trigger.new again, try using the caseList which are queried above.
Please find below updated code:
trigger updateChild on Case (after insert, after update){
    list<id> caseIds = new list<id>();
    list<id> childCaseIds = new list<id>();
    for(case c:trigger.new){
        caseIds.add(c.id);
    }
    
   list<case> caseList= [select id,(select id,Field_to_update__c from Casechilds__r)from case where id IN : caseIds ];
   system.debug('caseList'+caseList);
   system.debug('caseIds '+caseIds );
   for(case c : caseList){
       system.debug('c'+c);
       system.debug('c.Casechilds__r'+c.Casechilds__r);
       for(Case_child__c casechild :c.Casechilds__r){
           system.debug('casechild '+casechild );
           casechild.Field_to_update__c  = c.type;
           system.debug('casechild.Field_to_update__c'+casechild.Field_to_update__c);
           childCaseIds.add(casechild.id);
           system.debug('childCaseIds'+childCaseIds);
       }
      }
   // update childCaseIds;

}

Let me know if that helps you.

Best Regards,
BALAJI
Tarun SuriTarun Suri
Hi Manjunath,

You are trying to fetch a child record from the new parent record on insert thats why its giving null value. so to solve this problem write a trigger on update only cause on insert of parent record you wont be having any child record. Here is the sample code You an try
trigger updateChild on Case (after update) {
List<Case_child__c> conList = new List<Case_child__c>();
    
    for(Case acc : [select id , type , (select id , Field_to_update__c from Case_child__r) from Case where id =:trigger.new])
    {
        if(acc.Rating!=null)
        {
            for(Case_child__c  con : acc.Case_child__r)
            {
                con.Field_to_update__c = acc.type;
                conList.add(con);
            }
        }
    }
    update conList;
}

Hope it Solves Your Problem
Thanx
Tarun Suri
 
Onesh ReddyOnesh Reddy
Hi Manjunath,
trigger.new at line 11 is unable to get the casechilds.
Please find the Updated code below.
trigger updating_childrecords on Case (after insert, after update){
    list<id> caseIds = new list<id>();
    list<Case_child__c> childCaseIds = new list<Case_child__c>();
    for(case c:trigger.new){
        caseIds.add(c.id);
    }
    
   list<case> caseList= [select id,type, (select id,name,Field_to_Update__c from Casechilds__r)from case where id IN : caseIds ];
   system.debug('caseList'+caseList);
   system.debug('caseIds'+caseIds );
   for(case c : caselist){
       system.debug('c'+ c);
       system.debug('c.Casechilds__r '+c.Casechilds__r);
       for(Case_child__c casechild :c.Casechilds__r){
           system.debug('casechild '+casechild );
           casechild.Field_to_update__c = c.type;
           system.debug('casechild.Field_to_update__c '+casechild.Field_to_update__c);
           childCaseIds.add(casechild);
           system.debug('childCaseIds'+childCaseIds);
       }
      }
   update childCaseIds;

}

Please let me know if it helps you.

Best Regards,
Onesh.K