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

My Requirement is I had Created a TotalAmount field on Account Object, I need to sum of the opportunity amount for all the opportunities for that account. using Batch class
This is my code and i am not getting results
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 id,TotalAmount__c,(select amount from Opportunities) from account where id IN:accountIds];
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);
}
}
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 id,TotalAmount__c,(select amount from Opportunities) from account where id IN:accountIds];
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 are using IN:accountIds how you are getting this list of accountIds?
Thanks!
You can 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!
If you have cash in your hand, Real Estate Pakistan is a large platform where you can invest. It's a long-term investment, so you'll need to have some home furnishings on hand. Al Awan Properties (https://alawanproperties.com.pk) in Islamabad may assist you in making real estate investments in Pakistan, (https://alawanproperties.com.pk/blue-world-city/) particularly in Islamabad. Al Awan Properties will also contribute to the development of Bahria Town, which is set to open soon. Please visit (https://alawanproperties.com.pk) (https://alawanproperties.com.pk/societies/nova-city-islamabad/) for more information.