How to create field on account to act as rollup summry field?

253    Asked by amit_2689 in Salesforce , Asked on Apr 16, 2021

 I am trying to develop an apex trigger which will give me total amount of all child objects amount and it will act as a Roll up Summary field. I am getting below error,

Variable does not exist: AccountID at line 25 column 23

Trigger

trigger RollUpField on Contact (after insert,after update,after delete,after undelete) { Mapcounter=new Map(); Set cids=new Set(); if(Trigger.isInsert||Trigger.isUpdate) { for(Contact c:Trigger.new) { cids.add(c.AccountID); } } if(Trigger.isDelete) { for(Contact c:Trigger.old) { cids.add(c.AccountID); } } for(AggregateResult q:[Select AccountID,Sum(Count__c)Amount from Contact where AccountID IN:cids group by AccountID]);{ counter.put((ID)q.get(AccountID),(Double)q.get(Amount)); } List acc=new List(); for(Account a:[Select id,Total_Amount__c from Account where ID IN:Cids]) { Double total=counter.get(a.id); a.Total_Amount__c =total; acc.add(a); } update acc; }


Answered by Carl Paige

 You are getting error because your syntax is wrong:

This

  counter.put((ID)q.get(AccountID),(Double)q.get(Amount));

needs to be

  counter.put((ID)q.get('AccountID'),(Double)q.get('Amount'));

 Hope this will help you create field on account to act as rollup summry field



Your Answer

Interviews

Parent Categories