+ Start a Discussion
VIGNESH BALASUBRAMANIANVIGNESH BALASUBRAMANIAN 

Display sum of child value in parent

Hi Everyone,

What is Wrong with this code,while I'm trying to display sum of child value in parent getting this error when save child record

Error:Apex trigger rollupcount caused an unexpected exception, contact your administrator: rollupcount: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []: Trigger.rollupcount: line 41, column 1


trigger rollupcount on Contact (after  insert,after update,after delete,after undelete) {
    
    List <Account> ac=new List<Account>();
     set<id> setid=new set<id>();
    if(Trigger.isDelete) {
     for(Contact test:Trigger.Old) {
      
        setid.add(test.accountid);
    
     }   
   
   }
   else
   if
   (Trigger.isinsert || trigger.isundelete || trigger.isupdate) {

     for(Contact test:Trigger.New) {
      
        setid.add(test.accountid);   
    
     }
   }
   
     
   

 AggregateResult[] cnt=[SELECT sum(Number__c)sumamt FROM Contact where AccountId in :setid ];
    
 for(AggregateResult c:cnt)
 {
     
        account ac11=new  account();
            Integer count = Integer.valueOf(c.get('sumamt'));
               ac11.count__c=count;
            ac.add(ac11);
    
  }
        
      update ac;
        
    }    
    
Thanks & Best Regards,
Vignesh.B
    
    
 
Best Answer chosen by VIGNESH BALASUBRAMANIAN
VIGNESH BALASUBRAMANIANVIGNESH BALASUBRAMANIAN
Hi Guys,

Following code works perfectly........

trigger rollupcount on Contact (after  insert,after update,after undelete) {
        List <Account> ac=new List<Account>();
    Decimal count;
     set<id> setid=new set<id>();
   
     for(Contact test:Trigger.new) {
      
        setid.add(test.accountid); 
    List<account> acct=[SELECT id FROM account where Id in :setid ];
 AggregateResult[] cnt=[SELECT sum(Number__c)sumamt FROM Contact where AccountId in :setid ];
 for(AggregateResult c:cnt)
 {
     
      count=(Decimal)c.get('sumamt');  
  }
    for(account a:acct)
    {
        a.count__c=count;
        ac.add(a);
    }
     update ac;
    }
}

Thank you Everyone....................
 

All Answers

FearNoneFearNone
"ac" contains new elements (and they don't have database-id yet), therefore update is not the appropriate command. try insert-command.
Manohar kumarManohar kumar

FearNone is right, it doesnot have so you cant update.

you can use this code to do the same :

trigger rollupcount on Contact (after  insert,after update,after delete,after undelete) {
    
    List <Account> ac=new List<Account>();
     set<id> setid=new set<id>();
    if(Trigger.isDelete) {
     for(Contact test:Trigger.Old) {
      
        setid.add(test.accountid);
    
     }   
   
   }
   else
   if
   (Trigger.isinsert || trigger.isundelete || trigger.isupdate) {

     for(Contact test:Trigger.New) {
      
        setid.add(test.accountid);   
    
     }
   }
   

  List<account> acclist = [select id,(select id, Number__c from contacts) count__c from account where id in :setid ];
  for(account a:acclist) {
    decimal total = 0; 
    for(contact c:a.contacts) {
        total = total + c.Number__c;    
    
    
    }
    a.Count__c = total;
  
  }
  update acclist;   
}


Let me know if this works for you.

Thanks,

Manohar

VIGNESH BALASUBRAMANIANVIGNESH BALASUBRAMANIAN
@Manohar Its not working

Regards,
Vignesh.B
VIGNESH BALASUBRAMANIANVIGNESH BALASUBRAMANIAN
This is the error what im getting.........

User-added image
Ankur Saini 9Ankur Saini 9
Hi VIGNESH BALASUBRAMANIAN

try this:
 
trigger rollupcount on Contact (after  insert,after update,after delete,after undelete) {
    
    List <Account> ac=new List<Account>();
    set<id> setid=new set<id>();
    if(Trigger.isDelete) {
     for(Contact test:Trigger.Old) {    
        setid.add(test.accountid);   
     }    
   }
   else
   if(Trigger.isinsert || trigger.isupdate || trigger.isundelete) {

     for(Contact test:Trigger.New) {     
        setid.add(test.accountid);      
     }
   }
    
 for(account ac11:[SELECT count__c,(select Number__c FROM Contacts) from Account where id in :setid ]){ 
   decimal temp=0.00;   
       for(Contact c : ac11.contacts){
           temp=temp+c.Number__c;
       }
            ac11.count__c=temp;
            ac.add(ac11);   
  }        
      update ac;      
}


Thanks
Ankur Saini
VIGNESH BALASUBRAMANIANVIGNESH BALASUBRAMANIAN
Hi Ankur,

I used your syntax but get the following error,

User-added image
Ankur Saini 9Ankur Saini 9
Hi VIGNESH BALASUBRAMANIAN

try this:

 
trigger rollupcount on Contact (after  insert,after update,after delete,after undelete) {
    
    List <Account> ac=new List<Account>();
    set<id> setid=new set<id>();
    if(Trigger.isDelete) {
     for(Contact test:Trigger.Old) {    
        setid.add(test.accountid);   
     }    
   }
   else
   if(Trigger.isinsert || trigger.isupdate || trigger.isundelete) {

     for(Contact test:Trigger.New) {     
        setid.add(test.accountid);      
     }
   }
    
 for(account ac11:[SELECT Total_Amount__c,(select amount__c FROM Contacts) from Account where id in :setid ]){ 
     
   if(ac11.Total_Amount__c==null)
        ac11.Total_Amount__c = 0;
 
       for(Contact c : ac11.contacts){
          ac11.Total_Amount__c += c.amount__c!=null?c.amount__c:0;
       }
            ac.add(ac11);   
  }        
      update ac;      
}

Thanks
Ankur Saini
 
sukanya banekarsukanya banekar
Hi Vignesh,
Try with following code
 
trigger rollupcount on Contact (after  insert,after update,after delete,after undelete) {
    map<Id,Integer> mapAccIdContactSum = new map<Id,Integer>();
    List <Account> ac=new List<Account>();

    if(Trigger.isDelete) {
        Integer sumCon=0;
        for(Contact test: Trigger.Old) {
            if(string.valueof(test.Number__c) =='' && test.Number__c== NULL ){
                test.Number__c= 0;
                sumCon+= integer.valueOf(test.Number__c);
            }
            else{
                sumCon+=integer.valueOf(test.Number__c);
            }
            mapAccIdContactSum.put(test.accountid,sumCon);
            
        }   
    }
    else if (Trigger.isinsert || trigger.isundelete || trigger.isupdate) {
		integer sumCon=0;
		for(Contact test:Trigger.New) {
			if(string.valueof(test.Number__c) =='' && test.Number__c== NULL ){
				test.Number__c=0;
				sumCon+=integer.valueOf(test.Number__c);
			}
			else{
				sumCon+=integer.valueOf(test.Number__c);
			}
			setid.add(test.accountid);   
			mapAccIdContactSum.put(test.accountid,sumCon);
		}
	}
    ac=new List<Account>();
    for(Account c: [Select Id,count__c from Account where Id IN: mapAccIdContactSum.keySet()])
    {

        c.count__c=mapAccIdContactSum.get(c.Id);
        ac.add(c);
    }
    update ac;
}

I hope this will help you.

Thanks,
Sukanya
VIGNESH BALASUBRAMANIANVIGNESH BALASUBRAMANIAN
Hi Guys,

Following code works perfectly........

trigger rollupcount on Contact (after  insert,after update,after undelete) {
        List <Account> ac=new List<Account>();
    Decimal count;
     set<id> setid=new set<id>();
   
     for(Contact test:Trigger.new) {
      
        setid.add(test.accountid); 
    List<account> acct=[SELECT id FROM account where Id in :setid ];
 AggregateResult[] cnt=[SELECT sum(Number__c)sumamt FROM Contact where AccountId in :setid ];
 for(AggregateResult c:cnt)
 {
     
      count=(Decimal)c.get('sumamt');  
  }
    for(account a:acct)
    {
        a.count__c=count;
        ac.add(a);
    }
     update ac;
    }
}

Thank you Everyone....................
 
This was selected as the best answer