This document provides the complete code snippet for a Batch process designed to refresh account balances efficiently. This Batch can be scheduled to run automatically at specified intervals, ensuring that balance data remains up-to-date without manual intervention. Additionally, the query within the Batch is configurable, allowing you to target specific records that require balance updates based on your business criteria. This flexibility enables precise control over which records are processed, optimizing performance and data accuracy.
public class BatchBalanceRefreshProduct implements Database.Batchable<sObject>,Database.AllowsCallouts {
String sQuery = System.label.BatchRefreshBalanceQuery;
public BatchBalanceRefreshProduct(){}
public Database.QueryLocator start(Database.BatchableContext BC) {
return Database.getQueryLocator(sQuery);
}
public void execute(Database.BatchableContext BC, List<bankconnect__Bank_Verification__c> listBankVerification) {
for(bankconnect__Bank_Verification__c oBankVerification : listBankVerification) {
try{
bankconnect.AdminServices.refreshPlaidProduct('Balance', oBankVerification.Id);
}catch(Exception ex){
System.debug('Error occured :: ' + ex.getMessage());
}
oBankVerification.Balance_Refresh__c = false;
oBankVerification.Balance_Refresh_Date__c = System.today();
}
update listBankVerification;
}
public void finish(Database.BatchableContext BC) {
Integer count = bankconnect__Plaid_Common_Settings__c.getInstance('NextBalanceRefreshBatchRunTime') != null ? Integer.valueOf(bankconnect__Plaid_Common_Settings__c.getInstance('NextBalanceRefreshBatchRunTime').bankconnect__value__c ) : null;
if(count != null){
DateTime scheduledTime = DateTime.now().addMinutes( count );
String timeString = scheduledTime.format( 'yyyy-MM-dd HH:mm:ss' );
String cronString = scheduledTime.format( 's m H d M ? yyyy' );
System.schedule( 'Process BalanceRefresh Batch - ' + timeString + ' (' + Math.random() + ')', cronString, new SchedulableBalanceRefreshProduct () );
}
}
}
Test Class
@isTest
public class BatchBalanceRefreshProduct_Test {
@testSetup
static void createTestData() {
bankconnect__.TestDataUtil.insertPlaidCredentials();
bankconnect__.PostInstallScript oInstall = new bankconnect__.PostInstallScript();
List<bankconnect__Plaid_Common_Settings__c> oCommonSettings = oInstall.createPlaidCommonSetting();
bankconnect__Plaid_Common_Settings__c oPlaidCommonSetting = new bankconnect__Plaid_Common_Settings__c();
oPlaidCommonSetting.Name = 'NextBalanceRefreshBatchRunTime';
oPlaidCommonSetting.bankconnect__value__c = '30';
oCommonSettings.add(oPlaidCommonSetting);
if(oCommonSettings != null && oCommonSettings.size() > 0){
insert oCommonSettings;
}
bankconnect__Bank_Verification__c oBankVerification = new bankconnect__Bank_Verification__c();
oBankVerification.bankconnect__ApplicationName__c = 'Multi Service Fuel Card';
oBankVerification.bankconnect__First_Name__c = 'Mars';
oBankVerification.bankconnect__Last_Name__c = 'Test';
oBankVerification.bankconnect__Email__c = 'test@gmail.com';
oBankVerification.bankconnect__Plaid_Products__c = 'Auth;Balance';
oBankVerification.bankconnect__ParentRecordId__c = 'a1h2g000000gl0WAAQ';
oBankVerification.Balance_Refresh__c = true;
oBankVerification.Balance_Refresh_Request_Date__c = date.newInstance(2023, 01, 04);
oBankVerification.bankconnect__SkipAutoTriggerEmail__c = true;
insert oBankVerification;
}
@isTest
static void testCreateBalnceRefreshBatch(){
Test.startTest();
List<bankconnect__Bank_Verification__c> listBankVerification = [Select Id, Name, Balance_Refresh__c, Balance_Refresh_Date__c, Balance_Refresh_Request_Date__c from bankconnect__Bank_Verification__c where Balance_Refresh__c = true AND Balance_Refresh_Request_Date__c = LAST_N_DAYS : 1];
System.debug('listBankVerification size == ' + listBankVerification.size());
try {
BatchBalanceRefreshProduct balanceRefresh = new BatchBalanceRefreshProduct();
Database.executeBatch(balanceRefresh, 1);
} catch(exception ex) {
system.debug(' ex message '+ex.getMessage());
}
test.StopTest();
}
}
Schedulable Class
public with sharing class SchedulableBalanceRefreshProduct implements Schedulable {
public void execute(SchedulableContext sc) {
BatchBalanceRefreshProduct oBatch = new BatchBalanceRefreshProduct();
database.executebatch(oBatch, 1);
}
}
Custom Label- BatchRefreshBalanceQuery
Select Id, Name, Balance_Refresh__c, Balance_Refresh_Date__c, Balance_Refresh_Request_Date__c from bankconnect__Bank_Verification__c where Balance_Refresh__c = true AND Balance_Refresh_Request_Date__c = LAST_N_DAYS : 1