global class UpdateAccountCloseDealAmount implements Database.Batchable { global Database.QueryLocator start(Database.BatchableContext BC) { return Database.getQueryLocator('SELECT Id, CloseDealAmount__c, (SELECT Amount FROM Opportunities WHERE StageName = \'Closed Won\') FROM Account'); } global void execute(Database.BatchableContext BC, List scope) { for(Account acc : scope) { Decimal maxAmount = 0; for(Opportunity opp : acc.Opportunities) { if(opp.Amount > maxAmount) { maxAmount = opp.Amount; } } acc.CloseDealAmount__c = maxAmount; } update scope; } global void finish(Database.BatchableContext BC) {} }