You need to sign in to do that
Don't have an account?
VIGNESH 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
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
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
FearNone is right, it doesnot have so you cant update.
you can use this code to do the same :
Let me know if this works for you.
Thanks,
Manohar
Regards,
Vignesh.B
try this:
Thanks
Ankur Saini
I used your syntax but get the following error,
try this:
Thanks
Ankur Saini
Try with following code
I hope this will help you.
Thanks,
Sukanya
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....................