You need to sign in to do that
Don't have an account?
sudhirn@merunetworks.com
Batch not updating correct value
Hi,
I wrote a below batch program which is working but bug i noticed is its updating common value to all the accounts which should not happen
example : account_ 1 has two opportunity with amount = 200
account_ 2 has one opportunity with amount = 100
When i run below batch it is update 200 as common to account_1 and account_2 which is a bug it should actiually update 200 for account_1 and 100 for account_1
Please suggest me how to get this fixed
Thanks
Sudhir
I wrote a below batch program which is working but bug i noticed is its updating common value to all the accounts which should not happen
example : account_ 1 has two opportunity with amount = 200
account_ 2 has one opportunity with amount = 100
When i run below batch it is update 200 as common to account_1 and account_2 which is a bug it should actiually update 200 for account_1 and 100 for account_1
Please suggest me how to get this fixed
global class batchpreamount implements Database.Batchable<sObject> { global Database.QueryLocator start(Database.BatchableContext BC) { String query = 'Select Id, Name, Previous_Year_Closed_Opportunities__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)preamount from opportunity WHERE accountid in:accIds and isDeleted = false AND forecastcategoryname = 'commit' AND closedate = LAST_YEAR]; if (groupedResults[0].get('preamount') != NULL) { Decimal totalSum = Decimal.valueOf ( groupedResults[0].get('preamount') + ''); for(Account a : Scope) { a.Previous_Year_Closed_Opportunities__c = totalSum; } update scope; } if (groupedResults[0].get('preamount') == NULL) { for(Account a : Scope) { a.Previous_Year_Closed_Opportunities__c = NULL; } update scope; } } global void finish(Database.BatchableContext BC) { } }
Thanks
Sudhir
All Answers
Replace groupedResults[0] by for loop.
can you please show me a example
when running a batch program