0

I have a batch class which update the primary field on the quote object using batch size of 1. I need to do this in such a way that each batch is processed 30 seconds after the previous one(there needs to be a 30 second delay between each batch processing). Is there any way to achieve this?

Here is the batch class:

public class SR_SetPrimaryOnQuoteBatch implements Database.Batchable<sObject>,Database.AllowsCallouts{

public List<SBQQ__Quote__c> quotesToUpdate;

// Constructor to initialize the list of quotes to update
public SR_SetPrimaryOnQuoteBatch(List<SBQQ__Quote__c> quotes) {
    this.quotesToUpdate = quotes;
}

public Database.QueryLocator start(Database.BatchableContext context) {
    return Database.getQueryLocator([SELECT Id, SBQQ__Primary__c FROM SBQQ__Quote__c WHERE Id IN :quotesToUpdate]);
}

// Execute method to update the SBQQ__Primary__c field on SBQQ__Quote__c records
public void execute(Database.BatchableContext context, List<SBQQ__Quote__c> scope) {
    
    List<SBQQ__Quote__c> quotesToUpdateBatch = new List<SBQQ__Quote__c>();
    
    for (SBQQ__Quote__c quote : scope) {
        quote.SBQQ__Primary__c = true;
        quotesToUpdateBatch.add(quote);
    }
    
    // Update the records
    try{
        update quotesToUpdateBatch;
    }
    catch(Exception e){
        Backend_Error__c errRec = Z_BackendErrorUtil.logError('SR_LargeDealService','syncDealLocation',e.getMessage(),'','',true);

    }        
}

public void finish(Database.BatchableContext context) {

}

}

1
  • Why do you need a delay? Commented Sep 8, 2023 at 12:51

1 Answer 1

0

You can try --> DelayEvent.delay(30)

public class DelayEvent {

    public static void Delay(Integer delay){
        DateTime delayTill = DateTime.now().addSeconds(delay);
        while(DateTime.now() == delayTill);
    }
}
2
  • == should be <=
    – sfdcfox
    Commented Sep 8, 2023 at 13:32
  • this only works as long as delay is < async transaction CPU limit (60 secs) and is not a great use of a multi-tenant architecture
    – cropredy
    Commented Sep 8, 2023 at 22:07

You must log in to answer this question.

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