You need to sign in to do that
Don't have an account?
Christopher_J
Help returning AggregateResult in Batch Apex update.
I'm new to Apex and trying to understand how to accomplish a fairly simple task in Batch Apex. I'm trying to sum a field from a group of records and write it to a field on every account. My code looks is below. The bolded part is throwing the error Compile Error: Illegal assignment from LIST<AggregateResult> to Decimal at line 15 column 9. I understand what the error is but I'm not sure how to return the result correctly in this context.
Thanks for any help you can provide!
global class CalculateACPY implements Database.Batchable<sObject>
{
global Database.QueryLocator start(Database.BatchableContext BC)
{
String query = 'Select Id, Name, Active_Contract_Amount__c from Account';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Account> scope)
{
for(Account a : scope)
{
a.Active_Contracts_Per_Year__c = [select sum(amount_per_year__c) from contract where EndDate < TODAY and id =:a.id];
}
update scope;
}
global void finish(Database.BatchableContext BC)
{
}
}
Thanks for any help you can provide!
global class CalculateACPY implements Database.Batchable<sObject>
{
global Database.QueryLocator start(Database.BatchableContext BC)
{
String query = 'Select Id, Name, Active_Contract_Amount__c from Account';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Account> scope)
{
for(Account a : scope)
{
a.Active_Contracts_Per_Year__c = [select sum(amount_per_year__c) from contract where EndDate < TODAY and id =:a.id];
}
update scope;
}
global void finish(Database.BatchableContext BC)
{
}
}
AggregateResult[] agList = [SELECT SUM(amount_per_year__c) totalSum FROM contract where Enddate < TODAY and Id=:a.id];
Decimal totalSum = Decimal.valueOf (agList[0].get('totalSum'));
a.Active_Contracts_Per_Year__c = totalSum;
..as an aside, not advisable to have a SOQL within a for loop for bulk processing reasons.
global class CalculateACPY implements Database.Batchable<sObject>
{
global Database.QueryLocator start(Database.BatchableContext BC)
{
String query = 'Select Id, Name, Active_Contract_Amount__c from Account';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Account> scope)
{
for(Account a : scope)
{
AggregateResult[] agList = [SELECT SUM(amount_per_year__c) totalSum FROM contract where Enddate < TODAY and Id=:a.id];
Decimal totalSum = Decimal.valueOf (agList[0].get('totalSum'));
a.Active_Contracts_Per_Year__c = totalSum;
}
update scope;
}
global void finish(Database.BatchableContext BC)
{
}
}
Also if it's not advisible to do this in a batch. What is the best way to run a nightly calculation like this on related records and put it in a field on the common record?
Thanks again!
Try below code :-
If this helps,please mark it as best answer to help others :)
a.Active_Contracts_Per_Year__c = groupedResults[0].get('totalsum');
to
a.Active_Contracts_Per_Year__c = Decimal.Valueof(groupedResults[0].get('totalsum'));
global class CalculateACPY implements Database.Batchable<sObject>
{
global Database.QueryLocator start(Database.BatchableContext BC)
{
String query = 'Select Id, Name, Active_Contract_Amount__c from Account';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Account> scope)
{
List<Id> accIds = new List<Id>();
for(Account a : scope)
{
accIds.add(a.id);
}
// query outside for loop
List<AggregateResult> groupedResults = [SELECT sum(amount_per_year__c)totalsum from contract where EndDate < TODAY and id in:accIds];
for(Account a : Scope)
{
a.Active_Contracts_Per_Year__c = Decimal.Valueof(groupedResults[0].get('totalsum'));
}
update scope;
}
global void finish(Database.BatchableContext BC)
{
}
}
If this helps,please mark it as best answer to help other :)
Does it have something to do with the field types by any chance? They are set to number I believe not currency. I could change them however.
else let me know datatype of fields Active_Contracts_Per_Year__c and amount_per_year__c !!
The fields are:
Active_Contracts_Per_Year__c Number(18, 0)
Amount_per_year__c Formula (Number)
I could change these to currency if that would help. Is it that the field is a formula? I suppose I could overcome that with workflow field update if I needed to.
Thanks again!
a.active_contracts_per_year__c = Decimal.valueOf(groupedresults[0].get('totalsum'));
Can you verify that ??
global class CalculateCPY implements Database.Batchable<sObject>
{
global Database.QueryLocator start(Database.BatchableContext BC)
{
String query = 'Select Id, Name, Active_Contracts_Per_Year_Amount__c from Account';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Account> scope)
{
for(Account a : scope)
{
AggregateResult[] agList = [SELECT SUM(amount_per_year__c) totalSum FROM contract where Enddate > TODAY and Id=:a.id];
a.Active_Contracts_Per_Year_Amount__c = integer.valueOf (agList[0].get('totalSum'));
}
update scope;
}
global void finish(Database.BatchableContext BC)
{
}
}
I had a similar issue and found that converting to String and then to Decimal worked.
Try: Not sure how much this is still relevant to Christopher, but for future references... ;)