I have one batch job which delete old account associations then create new account associations & update fields[ For which logic is written in a trigger]
Previously it was taking less time, it executed within seconds, but now it is taking over 2 minutes.
global class AccountAssociationBatchJob implements Database.Batchable<sObject>,Schedulable{
global Database.QueryLocator start(Database.BatchableContext BC)
{
//To fetch the internal type of strategic accounts
String Acc = 'SELECT Id,Name,Account_Type__c,Strategic_account__c,AccountNumber,Ext_Cust_Id__c,GPO__c,IDN__c,Strategic__c,National__c FROM Account WHERE Account_Type__c = \'Internal\' AND (Ext_Cust_Id__c LIKE \'NA%\' OR AccountNumber LIKE \'NA%\') AND (GPO__c = true OR IDN__c = true OR Strategic__c = true OR National__c = true)';
return Database.getQueryLocator(Acc);
}
global void execute(Database.BatchableContext BC, List<Account> Ac)
{
Map<String, Id> accountMap = new Map<String, Id>();
list<string> Acc = new list<string>();
Set<string> StrategicAcc = new Set<string>();
list<string> StrategicAccAssociation = new list<string>();
Set<String> parentAccountId=new Set<String>();
List<Account> accountsToUpdate=new List<Account>();
for(Account a: Ac){
if(a.Account_Type__c == 'Internal' && a.Ext_Cust_Id__c != Null && a.AccountNumber != Null && a.Ext_Cust_Id__c.startsWith('NA') && a.AccountNumber.startsWith('NA') ){
StrategicAcc.add(a.Ext_Cust_Id__c);
}
else if(a.Account_Type__c == 'Internal' && a.Ext_Cust_Id__c == Null && a.AccountNumber != Null && a.AccountNumber.startsWith('NA') ){
StrategicAcc.add(a.AccountNumber);
}
else if(a.Account_Type__c == 'Internal' && a.Ext_Cust_Id__c != Null && a.Ext_Cust_Id__c.startsWith('NA')){
StrategicAcc.add(a.Ext_Cust_Id__c);
}
Acc.add(a.Id);
accountMap.put(a.Ext_Cust_Id__c, a.Id);
}
//To get the customer type of accounts where the Strategic_account__c matches with internal strategic account account number
List<Account> Acc1 = [SELECT Id,Name,Account_Type__c,Strategic_account__c,AccountNumber,Ext_Cust_Id__c FROM Account WHERE (Account_Type__c = 'Customer' OR Account_Type__c = 'Prospect') AND Strategic_account__c IN : StrategicAcc];
List<Account_Associations__c> AccAssociations = [SELECT Id,Account__c,Strategic__c From Account_Associations__c WHERE Strategic__c IN : Acc];
//Delete the existing associtions under all the internal strategic accounts
if(AccAssociations.size() > 0){
Delete AccAssociations;
}
list<Account_Associations__c> AccountAssociationslist = new list<Account_Associations__c>();
// Build the association records under internal accounts
for(Account a: Acc1){
Account_Associations__c AccAssociationrec1 = new Account_Associations__c();
if(a.Strategic_account__c != Null && a.Strategic_account__c.startsWith('NA')){
AccAssociationrec1.Strategic__c = accountMap.get(a.Strategic_account__c);
//Strategic__C is id of parent A/c, Add the id to list
parentAccountId.add(AccAssociationrec1.Strategic__c);
}
AccAssociationrec1.Account__c = a.Id;
AccAssociationrec1.Account_Role__c = 'Strategic';
AccountAssociationslist.add(AccAssociationrec1);
}
if(AccountAssociationslist.size() > 0){
insert AccountAssociationslist;
}
//Create a list of Accounts to update
//After update trigger is fired, Strategic_Account_Owner__c & Strategic_Account_Director__c of child accounts will be updated
accountsToUpdate=[SELECT Id FROM Account WHERE id IN: parentAccountId ];
if(accountsToUpdate.size() > 0){
update accountsToUpdate;
}
}
global void finish(Database.BatchableContext BC) {
}
global void execute(SchedulableContext sc)
{
Database.executebatch(new AccountAssociationBatchJob(),30);
}
}