How to get batch status using Apex code?

How to get batch status using Apex code?

Sample Code:


    public String status {get;set;}
    Id batchId;

/*Method to call the batch class*/
public void startBatch(){
        batchAccountUpdate obj = new batchAccountUpdate();
        batchid = Database.executebatch(obj);
    }
    
/*Method to check the status of batch*/
    public void checkStatus(){
        AsyncApexJob aaj = [SELECT Status FROM AsyncApexJob WHERE ID =: batchId];
                    
        status = aaj.Status;
    }

Cheers!!!

Leave a Reply