After the release of Summer’14, it is possible to call the flows from apex classes, using Flow. Interview Class and its start method to start the flow. You can use the flow in the Visualforce page also using flow:interview component but in this case, we need to use UI and User Interaction to drive it.
The Flows in Salesforce can be triggered ready flows means a flow that can be launched without user interaction. In this case, the flows are called using Workflow actions or Process Builder. Here we are just making the Flow and call it through apex code and a scheduled class which calls the controller method to execute the flow.
Flow Example
About Flow
In this flow, we are getting the record from Lead make a decision if all the required fields for a hot lead is not null then we are sending an email to the owner.
Also Read: How To Embed Lightning Component In Flow
When lead is going to be saved it goes to the record variable of flow that is “new Lead” and then we are saving it to Lead fields and then proceed to the next process.
Flow Controller
public class FlowController { public FlowController(){ //Intialization } public void callFlow(){ Lead l = new Lead(); l.FirstName = 'Sapna'; l.LastName = 'Chandani'; l.Email = '[email protected]'; l.Phone = '8522256355'; l.MobilePhone = '876545465'; l.Company = 'Emizentech'; insert l; Map<String,Lead> leadMap = new Map<String, Lead>(); leadMap.put('newLead', l); Flow.Interview.Email_Flow flow1 = new Flow.Interview.Email_Flow(leadMap); flow1.start(); } }
You can call this controller using VF Page
<apex:page controller="FlowController"> <apex:form > <apex:commandButton action="{!callFlow}" value="Start" reRender="text"/> </apex:form> </apex:page>
Schedulable Class
global class scheduledFlow implements Schedulable { global void execute(SchedulableContext sc) { FlowController fc = new FlowController(); fc.callFlow(); } }