You need to sign in to do that
Don't have an account?

Hi Everyone, I have Created a field called Total Amount on Account and I need to Populate the Total Amount on Account with the sum of the opportunity amounts for all the opportunities for that account Using Batch class
I have tried this below code its not working
someone please help me writing the batch class and test class for this scenerio
global class batchclass implements Database.Batchable<sObject>,Database.Stateful{
global Decimal sum;
/* Batch class constructor */
global batchclass(){}
//Start method
global Database.QueryLocator start(Database.BatchableContext BC)
{
String query='SELECT id, Amount from Opportunity';
return Database.getQueryLocator(query);
}
//Excute method
global void execute(Database.BatchableContext BC, List<Opportunity> scope)
{
//Excution logic
//List<Opportunity> opp= new List<Opportunities>();
AggregateResult[] gr= [SELECT SUM(Amount) optyamt FROM Opportunity];
for(AggregateResult ag:gr){
sum = (Decimal)ag.get('optyamt');
}
}
global void finish(Database.BatchableContext BC){
// Finish logic
system.debug(''+sum);
}
}
someone please help me writing the batch class and test class for this scenerio
global class batchclass implements Database.Batchable<sObject>,Database.Stateful{
global Decimal sum;
/* Batch class constructor */
global batchclass(){}
//Start method
global Database.QueryLocator start(Database.BatchableContext BC)
{
String query='SELECT id, Amount from Opportunity';
return Database.getQueryLocator(query);
}
//Excute method
global void execute(Database.BatchableContext BC, List<Opportunity> scope)
{
//Excution logic
//List<Opportunity> opp= new List<Opportunities>();
AggregateResult[] gr= [SELECT SUM(Amount) optyamt FROM Opportunity];
for(AggregateResult ag:gr){
sum = (Decimal)ag.get('optyamt');
}
}
global void finish(Database.BatchableContext BC){
// Finish logic
system.debug(''+sum);
}
}
i write trigger on above scneario Try it works and mark the best answer so it helps other.
handler:
public with sharing class CountOpportunityAmountt {
public void AfterinsertOpp(list<opportunity> newopp,list<opportunity> oldopp, map<Id,opportunity> NewMapOpp,map<Id,opportunity> OldMapopp){
set<Id> accountIds=new set<Id>();
for(opportunity o:newopp){
accountIds.add(o.accountId);
}
updateAmt(accountIds);
}
public void AfterUndeletetOpp(list<opportunity> newopp,list<opportunity> oldopp, map<Id,opportunity> NewMapOpp,map<Id,opportunity> OldMapopp){
set<Id> accountIds=new set<Id>();
for(opportunity o:newopp){
accountIds.add(o.accountId);
}
updateAmt(accountIds);
}
public void AfterUpdateOpp(list<opportunity> newopp,list<opportunity> oldopp, map<Id,opportunity> NewMapOpp,map<Id,opportunity> OldMapopp){
set<Id> accountIds=new set<Id>();
for(opportunity o:newopp){
if(o.amount !=OldMapopp.get(o.Id).amount){
accountIds.add(o.accountId);
}
accountIds.add(OldMapopp.get(o.Id).accountId);
}
updateAmt(accountIds);
}
public void AfterDeleteOpp(list<opportunity> newopp,list<opportunity> oldopp, map<Id,opportunity> NewMapOpp,map<Id,opportunity> OldMapopp){
set<Id> accountIds=new set<Id>();
for(opportunity o:Oldopp){
accountIds.add(o.accountId);
}
updateAmt(accountIds);
}
public static void updateAmt(set<Id> accountIds){
if(accountIds.size()>0){
decimal sum=0;
list<account> acclist=[select id,total_opportunity_amount__c,(select amount from Opportunities) from account where id IN:accountIds];
if(acclist.size()>0){
for(Account a:acclist){
for(opportunity o:a.Opportunities){
sum=sum+o.amount;
}
a.total_opportunity_amount__c=sum;
}
Update acclist;
}
}
}
}
trigger:
trigger Question16 on opportunity (after insert,after update,after delete,after Undelete ) {
CountOpportunityAmountt handler=new CountOpportunityAmountt();
if(Trigger.isInsert){
handler.AfterinsertOpp(Trigger.new,Trigger.old,Trigger.NewMap,Trigger.OldMap);
}
else if(Trigger.isUndelete){
handler.AfterUpdateOpp(Trigger.new,Trigger.old,Trigger.NewMap,Trigger.OldMap);
}
else if(Trigger.isUpdate){
handler.AfterdeleteOpp(Trigger.new,Trigger.old,Trigger.NewMap,Trigger.OldMap);
}
else if(Trigger.isdelete){
handler.AfterundeleteOpp(Trigger.new,Trigger.old,Trigger.NewMap,Trigger.OldMap);
}
}
Thanks
2. Change the Aggregate query in execute method like this
then loop through the aggregate results and update account.
thank you so much
can i get test class for it
Try Below Test Class Please Mark It As Best Answer If It Helps
Thank you
My batch class is having errors can u please help me with this
global class batchclass implements Database.Batchable<sObject>,Database.Stateful{
global Decimal sum;
/* Batch class constructor */
global batchclass(){}
//Start method
global Database.QueryLocator start(Database.BatchableContext BC)
{
String query='SELECT id, Amount from Opportunity';
return Database.getQueryLocator(query);
}
//Excute method
global void execute(Database.BatchableContext BC, List<Opportunity> scope)
{
//Excution logic
//List<Opportunity> opp= new List<Opportunities>();
AggregateResult[] gr= [SELECT AccountId,SUM(Amount) optyamt FROM Opportunity where accountId in: scopeIdList group by accountId];
system.debug(gr);
for(AggregateResult ag:gr){
sum = (Decimal)ag.get('optyamt');
system.debug(''+sum);
}
}
global void finish(Database.BatchableContext BC){
// Finish logic
system.debug(''+sum);
}
}
You have take reference from this below code:-
In case you find any other issue please mention.
If you find your Solution then mark this as the best answer.
Try Below Code With Test Class Please Mark It As Best Answer If It Helps
Thank You!
Because out of the box you can just create a Roll Up Summary field on the Account object that summarises values on the Opportunity.
Funny though as a test of skill that we go to the forums and ask others to write our code.
cheers
Andrew
wutung (https://www.wutungprinting.com/product/cosmetic-bottle-printing/)