Thursday 12 May 2016

Apex batch Auto Monitoring

Many a times you may have been in a situation where Salesforce Apex jobs gives a strong time and you are left with scratching your head due to Apex Job status unavailability. Salesforce Geeks can use an awesome object provided by Salesforce for Apex job Monitoring i.e.- AsyncApexJob . It is a backend object automatically maintained by Salesforce to track past/current Jobs. 

Although Salesforce has provided a great User Interface for same purpose i.e. Setup>Monitor>Jobs>Apex Jobs but sometimes it behaves strangely or you are not able to get proper status for your batch job, so in that case you may use Developer Console/Workbench(My personal favorite) to query AsyncApexJob object and check other details about your Apex Jobs

Lets assume your batch class is names "myBatch" then below SOQL would help you to get data related to your batch job Record

SELECT Id, MethodName, JobItemsProcessed, ApexClassId, ApexClass.Name, CompletedDate, NumberOfErrors, Status, ExtendedStatus, TotalJobItems FROM AsyncApexJob where ApexClass.Name='myBatch' order by CompletedDate Desc Limit 10

Above SOQL will return Status(and other fields) for Batch Job and hence you can identify the exact stage, number of records processed, Reason for failure etc.
********Hopefully it helps your Debugging********
Keep doing Great Work