You need to sign in to do that
Don't have an account?
Chitral Chadda
trigger to sum
sum__c field in contact and total_sum__c field in account
for all the contact fr a particular account
i want the total sum to be displayed in total_sum__c field
like in 1 contact sum__c field has 10
in other contact sum__c field is 20
so total_sum__c should be 30
i dont want to use aggregate funt and roll up summary?
any idea
trigger sum_Total_Sum on Contact (after insert,after update ,after delete)
{
set<id> conAccId = new set<id>();
if(trigger.isInsert || trigger.isUpdate)
{
for(contact c : trigger.new)
{
conAccId.add(c.AccountId);
}
}
if(trigger.isDelete)
{
for(contact c: trigger.old)
{
conAccId.add(c.AccountId);
}
}
List<account> accountToUpdate = new List<account>();
for( account ac :[select id,(select id, Sum__c from contacts ) from account where id IN : conAccId ])
{
ac.Total_Sum__c =ac.Total_Sum__c + c.Sum__c;
accountToUpdate.add(ac);
}
update accountToUpdate;
}
for all the contact fr a particular account
i want the total sum to be displayed in total_sum__c field
like in 1 contact sum__c field has 10
in other contact sum__c field is 20
so total_sum__c should be 30
i dont want to use aggregate funt and roll up summary?
any idea
trigger sum_Total_Sum on Contact (after insert,after update ,after delete)
{
set<id> conAccId = new set<id>();
if(trigger.isInsert || trigger.isUpdate)
{
for(contact c : trigger.new)
{
conAccId.add(c.AccountId);
}
}
if(trigger.isDelete)
{
for(contact c: trigger.old)
{
conAccId.add(c.AccountId);
}
}
List<account> accountToUpdate = new List<account>();
for( account ac :[select id,(select id, Sum__c from contacts ) from account where id IN : conAccId ])
{
ac.Total_Sum__c =ac.Total_Sum__c + c.Sum__c;
accountToUpdate.add(ac);
}
update accountToUpdate;
}
Reason : The error you are getting because contact is actually wrapper under account, so you will need to loop over that collection. Hope this solve the issue
All Answers
i know it can b done thru aggrgate quey brt i need to do this way
Reason : The error you are getting because contact is actually wrapper under account, so you will need to loop over that collection. Hope this solve the issue