0

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.log

Time taken Code

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);
    
}

}

5
  • 2
    Hi and welcome to SFSE. Your question isn't clear; what is the actual problem? Did you change something that caused this loss of performance? What data volumes are involved? Please edit the question to add more detail. Remember that Salesforce can throttle your async processes if you have too many that are using a lot of resources. It may or may not have been some code change that caused the performance degradation.
    – Phil W
    Oct 6, 2022 at 10:36
  • Thank You @PhilW ! This issue solved by removing unnecessary fields in String query. I only kept the fields which are used in the job. This improved execution time. Oct 6, 2022 at 12:35
  • @PhilW Can you tell me what is the ideal time for apex batch job execution. Is it alright if it take 2-3 minutes? Oct 6, 2022 at 12:39
  • How long a batch takes to execute depends on the data domain and the processing to be performed. A batch can process up to 50 million records (split into chunks, of course) and depending on what you are doing to these records the over-all execution could be literally days. Each chunk execution is limited to up to 60 seconds of CPU time. However, you have no control over how long it takes for the Salesforce infrastructure to get round to invoking the batch; async processes have no SLA. I've seen up to 15 minute delay on running a scheduled job, for example.
    – Phil W
    Oct 6, 2022 at 13:20
  • @PhilW Thank You for explaining!! Oct 6, 2022 at 13:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy