0

I have tried to put together a batch class to update parent Account records with credit scores.. I need to make 2 list variables public so I am able to access them in the finish global batch method, how would I do this? I get errors all lines in the finish method...

global class BatchCreditReportDelta implements Database.Batchable<sObject> {

public static void runBatch() {
    BatchCreditReportDelta batch = new BatchCreditReportDelta();
    Database.executeBatch(batch);
}

global Database.QueryLocator start(Database.BatchableContext bc) {
    return Database.getQueryLocator(
        [SELECT Id, Name, Wholesale_Last_Month_Negative__c, Wholesale_This_Month_Negative__c, Wholesale_Last_Month_Positive__c, Wholesale_This_Month_Positive__c, VoIP_Last_Month_Negative__c, VoIP_This_Month_Negative__c, VoIP_Last_Month_Positive__c, VoIP_This_Month_Positive__c, (SELECT Name, Credit__c FROM Voip_IDs__r), (SELECT Name, Credit__c FROM Wholesale__r) FROM Account WHERE Account_Branding__c != null ]);
}

global void execute(Database.BatchableContext bc, List <Account> scope) {
    // create 2 lists to split negative and positive scores
    List <Account> popNegScores = new List <Account>();
    List <Account> popPosScores = new List <Account>();
    for (Account acc: scope) {
        // populate voip values
        for (VoIP_ID__c v : acc.VoIP_IDs__r) {
            // if scores below zero, assign to neg list
            if (v.Credit__c < 0) {
                popNegScores.add(new Account(
                // populate scores on account record
                id = v.id,
                VoIP_Last_Month_Negative__c = v.credit__c));
            }
            // what about values = 0? 
            else {
                popPosScores.add(new Account(
                // populate scores on account record
                id = v.id,
                VoIP_Last_Month_Positive__c = v.credit__c));
            }
        }
        for (Wholesale__c w : acc.Wholesale__r) {
            // for negative wholesale values, assign to account
            if (w.Credit__c < 0) {
                popNegScores.add(new Account(
                id = w.id,
                Wholesale_Last_Month_Negative__c = w.credit__c));
            }
            // for positive values, assign to account (what about values = 0?)
            else {
                popPosScores.add(new Account(
                id = w.id,
                Wholesale_Last_Month_Positive__c = w.credit__c));
            }
        }
    }
}
global void finish(Database.BatchableContext bc) {
    // do updates in here
    update popNegScores;
    update popPosScores;
    // print out lists
    System.debug(popNegScores);
    System.debug(popPosScores);
}
}

Thank you!

1 Answer 1

1

Why would you need to update your list of accounts in the finish method rather than at the end of your execute method ? Because your batch is already running mainly on your list of accounts, you can update your account within the execute methods itself.

The blocker you are facing is probably around wholesaler which maybe should be included in your scope instead. Then when you loop through your account, differentiate if the account is a wholesaler (then w.credit__c) otherwise loop through VoIP_ID__c children of the account.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .