+ Start a Discussion
RJ12RJ12 

Test Class for Batch Apex that invokes a Flow

I wrote a batch apex that invokes a Flow interview. Somehow my Test class is not covering execute method. Can anyone help me out?
Batch Apex:
global class Lead_remind implements Database.Batchable<sObject>{
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        Date d = Date.today();
        String query = 'SELECT Id, OwnerId, Reminder_date__c, Rev_Date__c, Status FROM Lead where Status=\'Qualify\' AND Reminder_date__c=:d';
  return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC, List<Lead> scope)
    {
        for(Lead l : scope)
        {
            Map<String, Object> params = new Map<String, Object>();
            params.put('LId',l.Id);
            params.put('LeadOwnerId',l.OwnerId);
            params.put('BusinessDay_3',l.Reminder_date__c);
            params.put('BusinessDay_4',l.Rev_Date__c);
            Flow.Interview.Lead_Flow lFlow = new Flow.Interview.Lead_Flow(params);
            lFlow.start();       
        }
        update scope;
    }  
    global void finish(Database.BatchableContext BC)
    {
    }

}

Test Class:
 
@isTest
private class Lead_remind_Test {
static testMethod void testMethod1() 
    {
            Lead L= new Lead();
            L.OwnerId = '0055A00000A6s5pLAQ';
            L.LastName ='Lead Name';
            L.Company = 'Lead Company';
            L.Status = 'New'; 
            L.LeadSource = 'Social';
            L.Phone = '7364859473';
            insert L;
        
           L.Reminder_date__c = Date.today();
           L.Status = 'Qualify';
	   update L;
        
        Test.startTest();
            Lead_remind obj = new Lead_remind();
            DataBase.executeBatch(obj); 
        Test.stopTest();
    }
}

Reminder_date__c, Rev_Date__c are 'Date' fields. These fields will be populated by a workflow when Lead status is 'Qualify'.
pranav_sanvatsarkarpranav_sanvatsarkar
Hello RJ12,

Your code is appearing right. Can you debug to see if the scope in execute method has some values in it?

Thanks.
Raj VakatiRaj Vakati
I belive its an issue with you queyr 
 
SELECT Id, OwnerId, Status FROM Lead where Status='Qualify' AND Reminder_date__c=2018-10-31 00:00:00

You need to change your SOQL query in main class as below
 
Date d = Date.today();
String query = 'SELECT Id, OwnerId, Status FROM Lead where Status=\'Qualify\' AND Reminder_date__c='+d;
System.debug('query'+query);


this is the complete code
 
global class Lead_remind implements Database.Batchable<sObject>{
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        Date d = Date.today();
        String query = 'SELECT Id, OwnerId, Reminder_date__c, Rev_Date__c, Status FROM Lead where Status=\'Qualify\' AND Reminder_date__c='+d;
  return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC, List<Lead> scope)
    {
        for(Lead l : scope)
        {
            Map<String, Object> params = new Map<String, Object>();
            params.put('LId',l.Id);
            params.put('LeadOwnerId',l.OwnerId);
            params.put('BusinessDay_3',l.Reminder_date__c);
            params.put('BusinessDay_4',l.Rev_Date__c);
            Flow.Interview.Lead_Flow lFlow = new Flow.Interview.Lead_Flow(params);
            lFlow.start();       
        }
        update scope;
    }  
    global void finish(Database.BatchableContext BC)
    {
    }

}

 
RJ12RJ12

@Raj Vakati,

My requirement is, Reminder_date__c will be populated with some date by a workflow field update. I have to invoke the visual flow whenever the lead status is 'Qualify' and Reminder_date__c = today.

String query = 'SELECT Id, OwnerId, Reminder_date__c, Rev_Date__c, Status FROM Lead where Status=\'Qualify\' AND Reminder_date__c='+d;

Is this query will give the correct result?

May I know what Reminder_date__c='+d means?
RJ12RJ12

@pranav_sanvatsarkar

The code is working fine. The class executes and also invokes the flow. 

Raj VakatiRaj Vakati
You are passing the SOQL query string wrongly .. you need to use it as i specified '

If you keep it in to single quote all will be string and no date value will be populated 


 
Raj VakatiRaj Vakati
Try this code  .. it will wokrk and run the test class also
 
global class Lead_remind implements Database.Batchable<sObject>{
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        Date d = Date.today();
        String query = 'SELECT Id, OwnerId, Reminder_date__c, Rev_Date__c, Status FROM Lead where Status=\'Qualify\' AND Reminder_date__c='+d;
  return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC, List<Lead> scope)
    {
        for(Lead l : scope)
        {
            Map<String, Object> params = new Map<String, Object>();
            params.put('LId',l.Id);
            params.put('LeadOwnerId',l.OwnerId);
            params.put('BusinessDay_3',l.Reminder_date__c);
            params.put('BusinessDay_4',l.Rev_Date__c);
            Flow.Interview.Lead_Flow lFlow = new Flow.Interview.Lead_Flow(params);
            lFlow.start();       
        }
        update scope;
    }  
    global void finish(Database.BatchableContext BC)
    {
    }

}

 
RJ12RJ12

@Raj Vakati,

I'm not populating any date values in the code. I'm checking whether the Lead status is 'Qualify' and Reminder_date__c = today's date or not. 

If Reminder date is today's date and Status is Qualify then it should invoke the flow.

RJ12RJ12

@Raj Vakati,

Can you help me understand, with this query are we populating todays' date value to Reminder_date__c?

String query = 'SELECT Id, OwnerId, Reminder_date__c, Rev_Date__c, Status FROM Lead where Status=\'Qualify\' AND Reminder_date__c='+d;