+ Start a Discussion
TestingSFUserTestingSFUser 

I want to get child's record total amount from parent record

Hi all, 

     i have one field in child and parent, in child object i.e. bank_balance__C and in parent object i.e. Totalbankbalance__C. 
 For example,
             record1------- bank_balance__C =100
             record2------- bank_balance__C =100
             record3------- bank_balance__C =100
my required field is totalbankbalance__C =bank_balance__C+bank_balance__C+bank_balance__C
                             totalbankbalance__C=300.

can any one help me.
thanks in advance
Best Answer chosen by TestingSFUser
CharuDuttCharuDutt
Hii sukumar
Try Below Trigger
trigger NumberOfChild2 on Contact (After Insert,After Update,After Delete) {
List<Account> accList=new List<Account>();

    Set<Id> setAccIds = new Set<Id>();
    if(Trigger.isInsert){
         if(trigger.isAfter){
        for(Contact con : Trigger.new){
            if(con.AccountId != null){
            setAccIds.add(con.AccountId);
            	}
			}
		}
    } 
    system.debug('setAccIds ==> '+setAccIds);
    if(Trigger.isUpdate){
         if(trigger.isAfter){
        for(Contact con : Trigger.new){ 
            if(con.AccountId!=Trigger.oldMap.get(con.Id).AccountId){
               	setAccIds.add(con.AccountId);
                setAccIds.add(Trigger.oldMap.get(con.Id).AccountId);
            	}
          
			}        
        }
    }
    if(Trigger.isDelete){
        if(trigger.isAfter){
        for(Contact con : Trigger.old) { 
            if(con.AccountId != null){
            setAccIds.add(con.AccountId);
            	}
        	}
        }
    }    
    for(Account acc :[Select id,Totalbankbalance__c,Description ,(Select id,name,bank_balance__C  from contacts) from Account where Id in : setAccIds]){
			integer val = 0;
        for(Contact con : acc.Contacts){
            
            val += integer.valueOf(con.bank_balance__C );
            system.debug('====> ' +val);
        }
        system.debug(val);
        acc.Totalbankbalance__c= string.valueOf(val);
        acclist.add(acc);
        
    }
    if(acclist.size()>0){
        update accList;     
    }
}
Please Close Your Query By Marking it As Best Answer So It Helps Others In Future
Thank You!

All Answers

CharuDuttCharuDutt
Hii sukumar
Try Below Trigger
trigger NumberOfChild2 on Contact (After Insert,After Update,After Delete) {
List<Account> accList=new List<Account>();

    Set<Id> setAccIds = new Set<Id>();
    if(Trigger.isInsert){
         if(trigger.isAfter){
        for(Contact con : Trigger.new){
            if(con.AccountId != null){
            setAccIds.add(con.AccountId);
            	}
			}
		}
    } 
    system.debug('setAccIds ==> '+setAccIds);
    if(Trigger.isUpdate){
         if(trigger.isAfter){
        for(Contact con : Trigger.new){ 
            if(con.AccountId!=Trigger.oldMap.get(con.Id).AccountId){
               	setAccIds.add(con.AccountId);
                setAccIds.add(Trigger.oldMap.get(con.Id).AccountId);
            	}
          
			}        
        }
    }
    if(Trigger.isDelete){
        if(trigger.isAfter){
        for(Contact con : Trigger.old) { 
            if(con.AccountId != null){
            setAccIds.add(con.AccountId);
            	}
        	}
        }
    }    
    for(Account acc :[Select id,Totalbankbalance__c,Description ,(Select id,name,bank_balance__C  from contacts) from Account where Id in : setAccIds]){
			integer val = 0;
        for(Contact con : acc.Contacts){
            
            val += integer.valueOf(con.bank_balance__C );
            system.debug('====> ' +val);
        }
        system.debug(val);
        acc.Totalbankbalance__c= string.valueOf(val);
        acclist.add(acc);
        
    }
    if(acclist.size()>0){
        update accList;     
    }
}
Please Close Your Query By Marking it As Best Answer So It Helps Others In Future
Thank You!
This was selected as the best answer
TestingSFUserTestingSFUser
Thanks CharuDutt..
Suppose i assume Contact as child object and Account as Parent object from your above code...
now i want to write the code in Parent object.. i.e. on Account object..is it possible to write the code on Parent object for this requirement? if it possbile could you please modify and send it.
Thanks.
CharuDuttCharuDutt
Hii SuKumar
I Think It Will Not Work Properly Coz IF You Do  Update Parent Field On Child Nothing Will Happen on Parent Record Until You Also Update Parent Record 
You Can Try Roll-up Summary Field If You Want To Avoid Code Makr Roll-up Summary Field on Parent Object Select Sum option And Then Select Field Of Which You Want The Sum Of 

Make Sure Parent And Child Have Master-detail Relationship For This

Please Close Your Query By Marking it As Best Answer So It Helps Others In Future
Thank You!