+ Start a Discussion
Priyanka7Priyanka7 

sum a field of contact and show in account without using aggregate function

Hi,

I have a number field in Contact and i want to show the sum of the number field for each contact in Account. How should I use without aggregate function?

Best Answer chosen by Priyanka7
SandhyaSandhya (Salesforce Developers) 
Hi,

You may use trigger refer sample code.
 
trigger updateFee on Contact (after insert,after update,after delete, after undelete) {
   List<account> list_Account= new List<account>();
    set<Id> set_RC = new set<Id>();
    Decimal Sum;
    if(trigger.isinsert || trigger.isupdate || trigger.isundelete){
        for(Contact objRc: trigger.new){
            set_RC.add(objRc.accountId);
        }
    }
    else if(trigger.isdelete){
        for(Contact objRc: trigger.old){
            set_RC.add(objRc.accountId);
        }
    }
    
    for(account objApp : [SELECT Id,Name,(SELECT Id,Name,Fee__c FROM contacts ) FROM account WHERE Id IN: set_RC]){
        Sum=0;
        for(Contact objRc: objApp.contacts ){
            Sum+=(objRc.Fee__c!=null)?objRc.Fee__c:0 ;
        }
        
        objApp.Total_Fee__c = Sum;
        list_Account.add(objApp);
    }
    
    if(list_Account.size()>0){
        update list_Account;
    }
}

Please mark it as solved if my reply was helpful. It will make it available for other as the proper solution.
 
Best Regards
Sandhya
 

All Answers

SandhyaSandhya (Salesforce Developers) 
Hi,

You may use trigger refer sample code.
 
trigger updateFee on Contact (after insert,after update,after delete, after undelete) {
   List<account> list_Account= new List<account>();
    set<Id> set_RC = new set<Id>();
    Decimal Sum;
    if(trigger.isinsert || trigger.isupdate || trigger.isundelete){
        for(Contact objRc: trigger.new){
            set_RC.add(objRc.accountId);
        }
    }
    else if(trigger.isdelete){
        for(Contact objRc: trigger.old){
            set_RC.add(objRc.accountId);
        }
    }
    
    for(account objApp : [SELECT Id,Name,(SELECT Id,Name,Fee__c FROM contacts ) FROM account WHERE Id IN: set_RC]){
        Sum=0;
        for(Contact objRc: objApp.contacts ){
            Sum+=(objRc.Fee__c!=null)?objRc.Fee__c:0 ;
        }
        
        objApp.Total_Fee__c = Sum;
        list_Account.add(objApp);
    }
    
    if(list_Account.size()>0){
        update list_Account;
    }
}

Please mark it as solved if my reply was helpful. It will make it available for other as the proper solution.
 
Best Regards
Sandhya
 
This was selected as the best answer
Srikanth sfdc 32Srikanth sfdc 32
Hello Priyanka

Above code is not work for the update and delete events ..

I have shared a sample code. It will works well


trigger rollAmountwithoutAggregate on Contact (after insert,after update,after delete,after undelete) 
{
   set<id> setids = new set<id>();
   
   list<Account> acclist = new list<schema.account>();
   
   list<Account> accuplst = new list<schema.account>();
   
   list<Account> accdellst = new list<schema.account>();

   
   decimal sum;
   
   
   if(trigger.isinsert )
   {
      for(contact c : trigger.new)
      {
         setids.add(c.Accountid);
      
      }
   }
   
   
   if(trigger.isUndelete)    
   {
     for(contact c :trigger.new)
     {
       setids.add(c.Accountid);
     
     }
   }
   
       
   
    for(Account ac : [select id,name,RollupAmount__c,(select id,name,conAmount__c from contacts)from Account where id In:setids])    
   {
       sum = 0;
     
     for(contact ct : ac.contacts)
     {
        sum += ct.conAmount__c; 

        
     
     }
        ac.RollupAmount__c = sum;
      
        acclist.add(ac);

   }
  
    if(acclist.size()>0)
    {
       update acclist;
       
    }  
  
  
  
     if(trigger.isupdate)
     {
       
         for(contact c : trigger.old)
         {
            
             Account ad = [select id,name,RollupAmount__c from Account where id=:c.accountid];
         
             list<contact> clst = [select id,name,conAmount__c  from contact where accountid=:ad.id]; 
              
              sum = 0;
             
             for(contact ct1 : clst)
             {
               if(ct1.conAmount__c!=null)
               {
                  sum += ct1.conAmount__c;
             
               }
             }   
           
             ad.RollupAmount__c = sum; 
             
             accuplst.add(ad);
             
              
         } 
               
            update accuplst;

  
    }
  
     if(trigger.isDelete)
     {
        for(contact c : trigger.old)
        {
           setids.add(c.Accountid);
         
        }   
     
     for(Account at : [select id,name,RollupAmount__c,(select id,name,conAmount__c from contacts)from Account where id In:setids])
     {
        
        for(contact c : trigger.old)
        {
           at.RollupAmount__c -= c.conAmount__c;
           
           accdellst.add(at);
        }     
        
          
          
           
      } 
            
        update accdellst;
   }
  
  
  
}