function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sandy ksandy k 

How to get BatchableContext Jobid in test method

Hi Guys,

I am writing test class and my test code fails to cover below highlighted code and which inturn does not include execute method in the coverage. Let me know how to include code to fulfill below condition

public Database.QueryLocator start(Database.BatchableContext BC) {
        Integer limitInt = 10000;
        List<AsyncApexJob> aajList = new List<AsyncApexJob>();
                                if(lastJobId!=null && lastJobId!=BC.getJobId()) {
             aajList.addAll([SELECT Id, Status, CompletedDate FROM AsyncApexJob WHERE ID =: lastJobId and JobType!='BatchApexWorker' and Status not in ('Aborted','Completed','Failed')]);
if(lastJobId==null || aajList.isEmpty()) {
            lastJobId = BC.getJobId();

User-added image

the if condition in the above code is failing.




 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Sandy,

What is the variable lastJobId. I dont see the declaration and assignment for that variable. It would be hard for experts to answer without that information.

Thanks,
 
mukesh guptamukesh gupta
Hi SAndy,

You need to make sure what lastJobId is coming due to test class execution, so use below line and let me know what is your output

system.debug('lastJobId==>> '+lastJobId);

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 
sandy ksandy k
Hi Guys, Thanks for your response 
 lastJobId  is Id type variable and initialised with null