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
NAGAM VENKATA KRISHNA 8NAGAM VENKATA KRISHNA 8 

Hello everyone I want to calculate the amount in child records and display in parent "total amount" field. If in the child records "type" is LOP we have deduct the amount from total Amount. If "type" is Convience or HRC we have add it with total amount

It will calculate for every insert in child records
CharuDuttCharuDutt
Hii Nagam
Try Below trigger
trigger NumberOfChild 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 && con.Type == 'Convience ' && con.Type == 'HRC'){
            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  && con.Type == 'Convience' && con.Type == 'HRC' && con.Type != Trigger.oldMap.get(con.Id).Type && con.Type != Trigger.oldMap.get(con.Id).Type){
               	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  && con.Type == 'Convience ' && con.Type == 'HRC'){
            setAccIds.add(con.AccountId);
            	}
        	}
        }
    }    
    for(Account acc :[Select id,Total_Amount__c ,(Select id,Name,Amount__c from contacts) from Account where Id in : setAccIds]){
      integer iAmt = 0;
        acc.Total_Contacts__c = acc.contacts.size();
        for(Contact Con :acc.contacts){
            iAmt +=Con.Amount__c +',';
            system.Debug('s===>'+iAmt );
            
        }
        acc.Total_Amount__c  =  iAmt ;
        
        acclist.add(acc);
    }
    if(acclist.size()>0){
        update accList;     
    }
}
Please Mark It As Best Answer If It Helps
Thank You!