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
himanshu huske 7himanshu huske 7 

rollUp_Summery_trigger_task

RollUp Contact to Account.
Account has Total_Amt__c field, Customer has Amount__c(Currency) field. 
Total_Amt__c field should show all sum of all values of Amount__c field of all chield reccord
(please explain critical lines in code)
Best Answer chosen by himanshu huske 7
ASIF ALIASIF ALI
Hiiii Himanshu ,
Please check out the below code ,If you get any difficulty let me know
 
trigger totalAmount on Contact (after insert, after update, after undelete, after delete) {
    set<Id> accId = new set<Id>();
    map<Id,list<contact>> accMap = new map<Id,list<contact>>();
    list<account> acclist = new list<account>();
    list<contact> conList = new list<contact>();
    if(trigger.isInsert || trigger.isUpdate || trigger.isUndelete)
    {
        for(contact con : trigger.new)
        {
            if(con.accountId != null)
                accId.add(con.accountId);
        }
    }
    if(trigger.isDelete)
    {
        for(contact con : trigger.old)
        {
            if(con.AccountId != null)
                accId.add(con.AccountId);
        }
    }
    if(accId.size()>0)
    {
        
        conList = [Select Id,accountId,lastname,amount__c from contact where accountId IN:accId];
        for(contact con: conList)
        {
            if(!accMap.containsKey(con.AccountId))
            	accMap.put(con.AccountId, new list<contact>());
            accMap.get(con.AccountId).add(con);
            system.debug('This is working Partially');
        }
        accList = [Select Id, name, total_amount_on_contacts__c from account where Id IN: accMap.keySet()];
         decimal amount = 0;
        for(account acc : accList)
        {
            for(contact con: accMap.get(acc.Id))
            {
               if(con.Amount__c != null)
                   amount += con.Amount__c;
            } 
            acc.Total_amount_on_Contacts__c = amount;
            system.debug(amount);
        }
        update accList;
    }
}