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
Muhammad Jawwad 16Muhammad Jawwad 16 

Run flow from batch apex?

Hi friends,

I'm trying to run 5 flows from batch apex but the batch was not working as expected. Here Is my code

global class reminderTextAutomationBatch implements Database.Batchable<sObject> {
   
    global Database.QueryLocator start(Database.BatchableContext BC) {
       //String num = '+923417670738'; 
        String query = 'SELECT Id,Mogli_Number__c,Delivery_Date_Time__c,PCC_Other__c,Pick_Up_Date_Time__c,Prescription_Date__c,RecordTypeId,Status__c,Prescription_Length__c,Due_Date__c FROM Prescription__c';
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC, List<Prescription__c> PrescriptionList) {
       
        // process each batch of records

        Id GraymontPrescriptionRecordTypeId = Schema.SObjectType.Prescription__c.getRecordTypeInfosByName().get('Graymont Prescription').getRecordTypeId(); 
        
        for(Prescription__c Pres : PrescriptionList)
        {       
            
            Map<String, Object> params = new Map<String, Object>();
            params.put('recordId', Pres.Id); 
            if (Pres.Delivery_Date_Time__c != null && Pres.PCC_Other__c =='LIVE' && Pres.Pick_Up_Date_Time__c == null && 
                    Pres.RecordTypeID ==GraymontPrescriptionRecordTypeId && Pres.Status__c != 'Cancelled' && Pres.Prescription_Date__c == Date.today()+1 && Pres.Mogli_Number__c !=null) {
                        Flow.Interview.GM_Text_Reminder_Surgery_Confirmation myFlow = new Flow.Interview.GM_Text_Reminder_Surgery_Confirmation(params);
                        myFlow.start();
                        System.debug('mai andar agaya');
               }
            else if (Pres.Delivery_Date_Time__c == Date.today()+1 && (Pres.PCC_Other__c !='ORTHOCOR' || Pres.PCC_Other__c !='IN-CLINIC FITTERS') && Pres.Pick_Up_Date_Time__c == null && 
                    Pres.RecordTypeID ==GraymontPrescriptionRecordTypeId && Pres.Status__c != 'Cancelled' && Pres.Mogli_Number__c !=null ) {
                        Flow.Interview.GM_Text_Reminder_Delivery myFlow = new Flow.Interview.GM_Text_Reminder_Delivery(params);
                        myFlow.start();
                        System.debug('mai andar agaya1');
               }
            else if ((Pres.PCC_Other__c !='ORTHOCOR' || Pres.PCC_Other__c !='IN-CLINIC FITTERS') && Pres.Pick_Up_Date_Time__c == Date.today()+1 && 
                    Pres.RecordTypeID ==GraymontPrescriptionRecordTypeId && Pres.Status__c != 'Cancelled' && Pres.Mogli_Number__c !=null ) {
                        Flow.Interview.GM_Text_Reminder_Pick_Up myFlow = new Flow.Interview.GM_Text_Reminder_Pick_Up(params);
                        myFlow.start();
                        System.debug('mai andar agaya2');
               }
            else if (Pres.Due_Date__c == Date.today()+3 &&(Pres.PCC_Other__c !='ORTHOCOR' || Pres.PCC_Other__c !='IN-CLINIC FITTERS') && Pres.Pick_Up_Date_Time__c == null && Pres.Prescription_Length__c > 0
                    && Pres.RecordTypeID ==GraymontPrescriptionRecordTypeId && Pres.Status__c != 'Cancelled'&& Pres.Mogli_Number__c !=null ) {
                        Flow.Interview.GM_Text_Reminder_Due_Date myFlow = new Flow.Interview.GM_Text_Reminder_Due_Date(params);
                        myFlow.start();
                        System.debug('mai andar agaya3');
               }
            else if (Pres.Due_Date__c == Date.today()+3 && Pres.PCC_Other__c !='IN-CLINIC FITTERS' && Pres.Pick_Up_Date_Time__c == null && Pres.Prescription_Length__c > 0
                    && Pres.RecordTypeID ==GraymontPrescriptionRecordTypeId && Pres.Status__c != 'Cancelled'&& Pres.Mogli_Number__c !=null  ) {
                        Flow.Interview.GM_Text_Reminder_In_Clinic myFlow = new Flow.Interview.GM_Text_Reminder_In_Clinic(params);
                        myFlow.start();
                        System.debug('mai andar agaya4');
               }

           }
        }
    
    global void finish(Database.BatchableContext BC) {
    	// execute any post-processing operations
  }
}

Please tell me where I'm wrong in this code and suggest the best solution.
AbhishekAbhishek (Salesforce Developers) 
Hi,

Please check out the documentation for the Flow. Interview class here:

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/flow_interview_class.htm

They have examples for how to call a new Flow:

public class FlowController {

   //Instance of the Flow
   public Flow.Interview.doubler myFlow {get; set;}
   public Double value {get; set;}

   public Double getOutput() {
      if (myFlow == null) return null;
      return (Double)(myFlow.getVariableValue('v1'));
   }

   public void start() {
      Map<String, Object> myMap = new Map<String, Object>();
      myMap.put('v1', input);
      myFlow = new Flow.Interview.doubler(myMap);  <== Instantiate with params
      myFlow.start();                              <== Start flow!
   }
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks.