• Hemant Kumar 119
  • NEWBIE
  • 0 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 2
    Replies
How to develop connect two salesforce org using REST API  Solution should keep account record in sync between two org.means on the insert, update and delete the same operation should happen in 2nd org and vice versa.
Thanks,
Vijay
How to update no child count(Field) in parent account detail page its follow account hierarchy up to three levels using Trigger.

trigger NumberOfChieldCount on Account (after Insert, after Update, after delete) {
    Set<id> ids= new Set<id>();
    List<Account> acclist = new List<Account>();
    integer count = 0;
   if(Trigger.isInsert || Trigger.isUpdate){
        for(Account acc: Trigger.new){
            if(acc.ParentId!=null)
                ids.add(acc.ParentId);
            acclist.add(acc);
        }
    }
    
    if(Trigger.isDelete){
        for(Account acc: Trigger.old){
            if(acc.ParentId!=null)
                ids.add(acc.ParentId);
            acclist.add(acc);
        }
    }   
    if(ids.size()>0){
        List<Account> accchild = new List<Account>([select id,Parentid from Account where Parentid IN: ids]);
        List<Account> accparent = new List<Account>([select id,Nubmer_of_Chields__c from Account where id IN: ids]);
        /*integer nochilds=[select count() from Account where Parentid in :ids];
        system.debug('nochilds::::::::'+nochilds);
        integer noofchilds=[select count() from Account where id in :ids];
        system.debug('noofchilds::::::::'+noofchilds);
        system.debug('accchild 27-->'+accchild);
        system.debug('accparent 28-->'+accparent);
        integer noof;
        integer nofchids;
        integer nc;*/
        for(Account ac: accparent){
            count =0;
            for(Account acchilds: accchild){
                /*if(acchilds.Nubmer_of_Chields__c!=null && ac.Nubmer_of_Chields__c!=null)
                    nofchids=integer.valueof(acchilds.Nubmer_of_Chields__c)+integer.valueof(ac.Nubmer_of_Chields__c);
                if(acchilds.Nubmer_of_Chields__c!=null && ac.Nubmer_of_Chields__c==null)
                    nofchids=integer.valueof(acchilds.Nubmer_of_Chields__c);   
                
                system.debug('noof:::: 36'+nofchids );*/
                if(acchilds.Parentid == ac.id)
                    count++;
                   else
                    count--;
            }
            //ac.Nubmer_of_Chields__c =string.valueof(count); 
            /*system.debug('ac.Nubmer_of_Chields__c::::::::'+ac.Nubmer_of_Chields__c);
            system.debug('count:::: 47'+count);
            system.debug('count:::: 47'+nofchids);*/
            //if(count>0 && nofchids!=null)
                //ac.Nubmer_of_Chields__c =string.valueof(count+nofchids); 
            //if(count>0 && nofchids==null) 
               //if(ac.Parentid == ac.id)
                ac.Nubmer_of_Chields__c =string.valueof(count);
               
                    
            /*else
                ac.Nubmer_of_Chields__c =string.valueof(count);  */         

        }
        
        try{
            upsert accparent;
        }catch(DMLException ex){
            System.debug('Exception is '+ex);
        }
    }
}
Note:Create  new field Nubmer_of_Chields__c in Account(Object).

A-->No of child are 2+1 =3
   B--->2
      c---->1
 
How to update no child count(Field) in parent account detail page its follow account hierarchy up to three levels using Trigger.

trigger NumberOfChieldCount on Account (after Insert, after Update, after delete) {
    Set<id> ids= new Set<id>();
    List<Account> acclist = new List<Account>();
    integer count = 0;
   if(Trigger.isInsert || Trigger.isUpdate){
        for(Account acc: Trigger.new){
            if(acc.ParentId!=null)
                ids.add(acc.ParentId);
            acclist.add(acc);
        }
    }
    
    if(Trigger.isDelete){
        for(Account acc: Trigger.old){
            if(acc.ParentId!=null)
                ids.add(acc.ParentId);
            acclist.add(acc);
        }
    }   
    if(ids.size()>0){
        List<Account> accchild = new List<Account>([select id,Parentid from Account where Parentid IN: ids]);
        List<Account> accparent = new List<Account>([select id,Nubmer_of_Chields__c from Account where id IN: ids]);
        /*integer nochilds=[select count() from Account where Parentid in :ids];
        system.debug('nochilds::::::::'+nochilds);
        integer noofchilds=[select count() from Account where id in :ids];
        system.debug('noofchilds::::::::'+noofchilds);
        system.debug('accchild 27-->'+accchild);
        system.debug('accparent 28-->'+accparent);
        integer noof;
        integer nofchids;
        integer nc;*/
        for(Account ac: accparent){
            count =0;
            for(Account acchilds: accchild){
                /*if(acchilds.Nubmer_of_Chields__c!=null && ac.Nubmer_of_Chields__c!=null)
                    nofchids=integer.valueof(acchilds.Nubmer_of_Chields__c)+integer.valueof(ac.Nubmer_of_Chields__c);
                if(acchilds.Nubmer_of_Chields__c!=null && ac.Nubmer_of_Chields__c==null)
                    nofchids=integer.valueof(acchilds.Nubmer_of_Chields__c);   
                
                system.debug('noof:::: 36'+nofchids );*/
                if(acchilds.Parentid == ac.id)
                    count++;
                   else
                    count--;
            }
            //ac.Nubmer_of_Chields__c =string.valueof(count); 
            /*system.debug('ac.Nubmer_of_Chields__c::::::::'+ac.Nubmer_of_Chields__c);
            system.debug('count:::: 47'+count);
            system.debug('count:::: 47'+nofchids);*/
            //if(count>0 && nofchids!=null)
                //ac.Nubmer_of_Chields__c =string.valueof(count+nofchids); 
            //if(count>0 && nofchids==null) 
               //if(ac.Parentid == ac.id)
                ac.Nubmer_of_Chields__c =string.valueof(count);
               
                    
            /*else
                ac.Nubmer_of_Chields__c =string.valueof(count);  */         

        }
        
        try{
            upsert accparent;
        }catch(DMLException ex){
            System.debug('Exception is '+ex);
        }
    }
}
Note:Create  new field Nubmer_of_Chields__c in Account(Object).

A-->No of child are 2+1 =3
   B--->2
      c---->1