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
PraveenchanduPraveenchandu 

Create 3 custom Objects a --> b -->c c is grand child a is grand parent display the count of c in grandparent A using trigger in lookup relation

Create 3 custom Objects
a --> b -->c
c is grand child
a is grand parent
display the count of c in grandparent A
using trigger
in lookup relation
Best Answer chosen by Praveenchandu
Khan AnasKhan Anas (Salesforce Developers) 
Hi Praveen,

Greetings to you!

Below is the sample code which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Grand Parent: Grand_Parent__c
Child of Grand Parent: Child_Of_GP__c
Grand Child (Child of Child_Of_GP__c): Grand_Child__c
trigger CountGrandChild on Grand_Child__c (after insert, after update, after delete) {
    
    Set<Id> oppIdSet = new Set<Id>();
    Set<Id> accIdSet = new Set<Id>();
    
    List<Grand_Parent__c> accToUpdate = new List<Grand_Parent__c>();
    
    if(Trigger.isInsert){
        for(Grand_Child__c oli: trigger.new){
            oppIdSet.add(oli.Child_Of_GP__c);
        }
    }
    
    if(Trigger.isUpdate|| Trigger.isDelete){
        for(Grand_Child__c oli:trigger.old){
            oppIdSet.add(oli.Child_Of_GP__c);
        }
    }
    
    for(Child_Of_GP__c oppty : [SELECT ID,Grand_Parent__c FROM Child_Of_GP__c WHERE Id IN: oppIdSet]){
        accIdSet.add(oppty.Grand_Parent__c) ;
    }
    List<AggregateResult> aggrs = [SELECT Child_Of_GP__r.Grand_Parent__c, count(Id)oli FROM Grand_Child__c 
                                   WHERE Child_Of_GP__r.Grand_Parent__c IN:accIdSet GROUP BY Child_Of_GP__r.Grand_Parent__c] ;
    if(aggrs.size() > 0){
        for(AggregateResult aggr : aggrs){
            Grand_Parent__c accObj = new Grand_Parent__c();
            accObj.Id = (id)aggr.get('Grand_Parent__c');
            accObj.Count_Of_GrandChild__c =(decimal)aggr.get('oli') ;
            accToUpdate.add(accObj);
        }
        System.debug('accToUpdate -> ' + accToUpdate);
        update accToUpdate;
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Praveen,

Greetings to you!

Below is the sample code which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Grand Parent: Grand_Parent__c
Child of Grand Parent: Child_Of_GP__c
Grand Child (Child of Child_Of_GP__c): Grand_Child__c
trigger CountGrandChild on Grand_Child__c (after insert, after update, after delete) {
    
    Set<Id> oppIdSet = new Set<Id>();
    Set<Id> accIdSet = new Set<Id>();
    
    List<Grand_Parent__c> accToUpdate = new List<Grand_Parent__c>();
    
    if(Trigger.isInsert){
        for(Grand_Child__c oli: trigger.new){
            oppIdSet.add(oli.Child_Of_GP__c);
        }
    }
    
    if(Trigger.isUpdate|| Trigger.isDelete){
        for(Grand_Child__c oli:trigger.old){
            oppIdSet.add(oli.Child_Of_GP__c);
        }
    }
    
    for(Child_Of_GP__c oppty : [SELECT ID,Grand_Parent__c FROM Child_Of_GP__c WHERE Id IN: oppIdSet]){
        accIdSet.add(oppty.Grand_Parent__c) ;
    }
    List<AggregateResult> aggrs = [SELECT Child_Of_GP__r.Grand_Parent__c, count(Id)oli FROM Grand_Child__c 
                                   WHERE Child_Of_GP__r.Grand_Parent__c IN:accIdSet GROUP BY Child_Of_GP__r.Grand_Parent__c] ;
    if(aggrs.size() > 0){
        for(AggregateResult aggr : aggrs){
            Grand_Parent__c accObj = new Grand_Parent__c();
            accObj.Id = (id)aggr.get('Grand_Parent__c');
            accObj.Count_Of_GrandChild__c =(decimal)aggr.get('oli') ;
            accToUpdate.add(accObj);
        }
        System.debug('accToUpdate -> ' + accToUpdate);
        update accToUpdate;
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
FARSANA PSFARSANA PS
Hai Praveen,

Try this code ,

Grand Parent: cObj
Child of Grand Parent: aObj
Grand Child : aObj
countGrantChild__c : Field to show count of cObj  in grandparent aObj
trigger cObj_trigger on cObj (after insert,after delete) {
   map<id,bObj> b_map=map<id,bObj>([select id,name,a from bObj]); 
   map<id,aObj> a_map= map<id,aObj>([select id,name,countGrantChild__c from aObj]);
   list<aObj> update_a_list=new  list<aObj>();
   aObj new_A=new aObj();
   bObj new_B=new bObj();
    for (cObj cc : Trigger.new) {
        if(b_map.get(cc.b)!=null)
        {
          new_B=b_map.get(cc.b);  
          if(a_map.get(new_B.a)!=null)
          {
            new_A=a_map.get(new_B.a);
            if(new_A.countGrantChild__c==null)
                new_A.countGrantChild__c=0;
            else
            {
                if(Trigger.isDelete)
                  new_A.countGrantChild__c= new_A.countGrantChild__c-1;
                if(Trigger.isInsert)
                     new_A.countGrantChild__c= new_A.countGrantChild__c+1;
                update_a_list.add(new_A) ;
            }

          }
        }
    }
    if(update_a_list.size>0)
        update update_a_list;

}
Hope this helps..
 
PraveenchanduPraveenchandu
Hi FARSANA,

Thanks for Your Time.
I'm getting this error at line 2

Error: Compile Error: Unexpected token '<'. at line 2 column 7



trigger cObj_trigger on Grand_Child_c__c (after insert,after delete) {
   map<id,Child_Of_GP_c__c> b_map=map <id,Child_Of_GP_c__c> ([select id,name,a from Child_Of_GP_c__c]); 
   
   map<id,Grand_Parent_c__c> a_map= map<id,Grand_Parent_c__c> ([select id,name,countGrantChild__c from Grand_Parent_c__c]);
   
   list<Grand_Parent_c__c> update_a_list=new  list<Grand_Parent_c__c>();
   
   Grand_Parent_c__c new_A=new Grand_Parent_c__c();
   
   Child_Of_GP_c__c new_B=new Child_Of_GP_c__c();
   
    for (Grand_Child_c__c cc : Trigger.new) {
        if(b_map.get(cc.b)!=null)
        {
          new_B=b_map.get(cc.b);  
          if(a_map.get(new_B.a)!=null)
          {
            new_A=a_map.get(new_B.a);
            if(new_A.countGrantChild__c==null)
                new_A.countGrantChild__c=0;
            else
            {
                if(Trigger.isDelete)
                  new_A.countGrantChild__c= new_A.countGrantChild__c-1;
                if(Trigger.isInsert)
                     new_A.countGrantChild__c= new_A.countGrantChild__c+1;
                update_a_list.add(new_A) ;
            }

          }
        }
    }
    if(update_a_list.size>0)
        update update_a_list;

}
FARSANA PSFARSANA PS
Hai Praveen,

Change these two lines
map<id,Child_Of_GP_c__c> b_map=map <id,Child_Of_GP_c__c> ([select id,name,a from Child_Of_GP_c__c]); 
   
 map<id,Grand_Parent_c__c> a_map= map<id,Grand_Parent_c__c> ([select id,name,countGrantChild__c from Grand_Parent_c__c]);

With these lines(Sorry I missed new keyword)
 
map<id,Child_Of_GP_c__c> b_map=new  map <id,Child_Of_GP_c__c> ([select id,name,a from Child_Of_GP_c__c]); 
   
 map<id,Grand_Parent_c__c> a_map=new map<id,Grand_Parent_c__c> ([select id,name,countGrantChild__c from Grand_Parent_c__c]);

Hope this helps...
 
PraveenchanduPraveenchandu
Thank you But.
Sorry again i'm getting the bellow Error.
Error: Compile Error: Variable does not exist: b at line 14 column 25
PraveenchanduPraveenchandu
Yes I came to know that 'new' keyword was missing But Variable B and A is not declared.
I'm getting the below error
Error: Compile Error: Variable does not exist: b at line 14 column 25
Thank You.
 
PraveenchanduPraveenchandu
trigger cObj_trigger on Grand_Parent_c__c(after insert,after delete) {
  map<id,Child_Of_GP_c__c> b_map = new map<id,Child_Of_GP_c__c>([select id,name from Child_Of_GP_c__c]);
   
   
   map<id,Grand_Parent_c__c> a_map= new map<id,Grand_Parent_c__c>([select id,name,countGrantChild_c__c from Grand_Parent_c__c]);
   
   list<Grand_Parent_c__c> update_a_list=new  list<Grand_Parent_c__c>();
   
   Grand_Parent_c__c new_A=new Grand_Parent_c__c();
   
   Child_Of_GP_c__c new_B=new Child_Of_GP_c__c();
   
    for (Grand_Child_c__c cc : Trigger.new) {
        if(b_map.get(cc.b)!=null)
        {
          new_B=b_map.get(cc.b);  
          if(a_map.get(new_B.a)!=null)
          {
            new_A=a_map.get(new_B.a);
            if(new_A.countGrantChild__c==null)
                new_A.countGrantChild__c=0;
            else
            {
                if(Trigger.isDelete)
                  new_A.countGrantChild__c= new_A.countGrantChild__c-1;
                if(Trigger.isInsert)
                     new_A.countGrantChild__c= new_A.countGrantChild__c+1;
                update_a_list.add(new_A) ;
            }

          }
        }
    }
    if(update_a_list.size>0)
        update update_a_list;

}

 
FARSANA PSFARSANA PS
Hai Praveen,

Please update 
map<id,Child_Of_GP_c__c> b_map=new  map <id,Child_Of_GP_c__c> ([select id,name,a from Child_Of_GP_c__c]);
Replace 'a' in query with your corresponding lookup field(relationship to Grand_Parent_c__c)  in Child_Of_GP_c__c
 
if(b_map.get(cc.b)!=null)
        {
          new_B=b_map.get(cc.b);  
          if(a_map.get(new_B.a)!=null)
          {
            new_A=a_map.get(new_B.a);
            if(new_A.countGrantChild__c==null)
                new_A.countGrantChild__c=0;
            else
            {
                if(Trigger.isDelete)
                  new_A.countGrantChild__c= new_A.countGrantChild__c-1;
                if(Trigger.isInsert)
                     new_A.countGrantChild__c= new_A.countGrantChild__c+1;
                update_a_list.add(new_A) ;
            }

          }
        }

Replace  every cc.b with  your corresponding lookup field(relationship to Child_Of_GP_c__c)  in Grand_Child_c__c 
   eg: if your lookup field api name is Child_Of_GP_c__c  then use cc.Child_Of_GP_c__c  instead of cc.b

Replace every cc.a with  your corresponding lookup field(relationship to Grand_Parent_c__c)  in Child_Of_GP_c__c
  eg:if your lookup field api name is Grand_Parent_c__cthen use cc.Grand_Parent_c__c instead of cc.a

Here a & b are my lookup field in my child and parent objects 


Hope this helps...
 
PraveenchanduPraveenchandu
Hi Farsana,
Thank You for ur help but unfortunatly again its not working.
I'm getting the following errors.

Error: Compile Error: Variable does not exist: countGrantChild_c__c at line 29 column 37

Error: Compile Error: Variable does not exist: size at line 38 column 26
 
trigger cObj_trigger on Grand_Child_c__c(after insert,after delete) {
  map<id,Child_Of_GP_c__c> b_map = new map<id,Child_Of_GP_c__c>([select id,name,Grand_Parent_c__c from Child_Of_GP_c__c]);
   
   
   map<id,Grand_Parent_c__c> a_map= new map<id,Grand_Parent_c__c>([select id,name,countGrantChild_c__c from Grand_Parent_c__c]);
   
   list<Grand_Parent_c__c> update_a_list=new  list<Grand_Parent_c__c>();
   
   Grand_Parent_c__c new_A=new Grand_Parent_c__c();
   
   Child_Of_GP_c__c new_B=new Child_Of_GP_c__c();
   
    for (Grand_Child_c__c cc : Trigger.new) {
        if(b_map.get(cc.Child_Of_GP_c__c)!=null)
        {
          new_B=b_map.get(cc.Child_Of_GP_c__c);  
          if(a_map.get(new_B.Grand_Parent_c__c)!=null)
          {
            new_A=a_map.get(new_B.Grand_Parent_c__c);
            if(new_A.countGrantChild_c__c==null)
                new_A.countGrantChild_c__c=0;
            else
            {
                if(Trigger.isDelete)
                  new_A.countGrantChild_c__c= new_A.countGrantChild_c__c-1;
                if(Trigger.isInsert)
                     new_A.countGrantChild_c__c= new_A.countGrantChild_c__c+1;
                update_a_list.add(new_A) ;
                
            }
            

          }
        }
       
     
    }
        if(update_a_list.size>0)
         update update_a_list;

}

 
PraveenchanduPraveenchandu
Hi Khan Anas,

Thank you for ur help But unfortunately it is not working here is the code @Khan Anas
trigger CountGrandChild on Grand_Child_c__c (after insert, after update, after delete) {
    
    Set<Id> oppIdSet = new Set<Id>();
    Set<Id> accIdSet = new Set<Id>();
    
    List<Grand_Parent_c__c> accToUpdate = new List<Grand_Parent_c__c>();
    
    if(Trigger.isInsert){
        for(Grand_Child_c__c oli: trigger.new){
            oppIdSet.add(oli.Child_Of_GP_c__c);
        }
    }
    
    if(Trigger.isUpdate|| Trigger.isDelete){
        for(Grand_Child_c__c oli:trigger.old){
            oppIdSet.add(oli.Child_Of_GP_c__c);
        }
    }
    
    for(Child_Of_GP_c__c oppty : [SELECT ID,Grand_Parent_c__c FROM Child_Of_GP_c__c WHERE Id IN: oppIdSet]){
        accIdSet.add(oppty.Grand_Parent_c__c) ;
    }
    List<AggregateResult> aggrs = [SELECT Child_Of_GP_c__r.Grand_Parent_c__c, count(Id)oli FROM Grand_Child_c__c
                                   WHERE Child_Of_GP_c__r.Grand_Parent_c__c IN:accIdSet GROUP BY Child_Of_GP_c__r.Grand_Parent_c__c] ;
    if(aggrs.size() > 0){
        for(AggregateResult aggr : aggrs){
            Grand_Parent_c__c accObj = new Grand_Parent_c__c();
            accObj.Id = (id)aggr.get('Grand_Parent_c__c');
            accObj.countGrantChild_c__c =(decimal)aggr.get('oli') ;
            accToUpdate.add(accObj);
        }
        System.debug('accToUpdate -> ' + accToUpdate);
        update accToUpdate;
    }
}