{"id":2899,"date":"2023-11-21T10:59:24","date_gmt":"2023-11-21T10:59:24","guid":{"rendered":"https:\/\/www.emizentech.com\/blog\/?p=2899"},"modified":"2023-11-21T10:59:33","modified_gmt":"2023-11-21T10:59:33","slug":"call-flow-from-apex","status":"publish","type":"post","link":"https:\/\/multisitelocal.ezxdemo.com\/blog\/call-flow-from-apex.html","title":{"rendered":"How To Call Flows From Apex In Salesforce"},"content":{"rendered":"\n<p>After the release of Summer &#8217;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 the flow: interview component, but in this case, we need to use UI and User Interaction to drive it.<\/p>\n\n\n\n<p>The Flows in Salesforce can be triggered. Ready flows mean 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 calling it through apex code and a scheduled class, which calls the controller method to execute the flow.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_81 counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #007db2;color:#007db2\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #007db2;color:#007db2\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/multisitelocal.ezxdemo.com\/blog\/call-flow-from-apex.html#Prerequisites\" >Prerequisites<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/multisitelocal.ezxdemo.com\/blog\/call-flow-from-apex.html#Call_Flows_From_Apex_Example\" >Call Flows From Apex Example<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/multisitelocal.ezxdemo.com\/blog\/call-flow-from-apex.html#About_Call_Flows_in_Salesforce\" >About Call Flows in Salesforce<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/multisitelocal.ezxdemo.com\/blog\/call-flow-from-apex.html#Step-by-Step_Guide_with_Code_Examples\" >Step-by-Step Guide with Code Examples<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/multisitelocal.ezxdemo.com\/blog\/call-flow-from-apex.html#Best_Practices_To_Follow_While_Calling_Flows_From_Apex_In_Salesforce\" >Best Practices To Follow While Calling Flows From Apex In Salesforce<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/multisitelocal.ezxdemo.com\/blog\/call-flow-from-apex.html#Troubleshooting_Common_Issues\" >Troubleshooting Common Issues<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/multisitelocal.ezxdemo.com\/blog\/call-flow-from-apex.html#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Prerequisites\"><\/span>Prerequisites<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>Salesforce Setup:<\/strong> <\/h3>\n\n\n\n<p>Ensure you have access to Salesforce with the necessary permissions to create and modify Flows and Apex classes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>Basic Knowledge:<\/strong> <\/h3>\n\n\n\n<p>Familiarity with Salesforce\u2019s Flow Builder and basic Apex coding is required to follow this guide effectively.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Call_Flows_From_Apex_Example\"><\/span>Call Flows From Apex Example<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/sites\/2\/2020\/05\/Flow-Example.png\" alt=\"Flow Example\" class=\"wp-image-2903\"\/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"About_Call_Flows_in_Salesforce\"><\/span>About Call Flows in Salesforce<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In this flow, we are getting the record from the Lead to make a decision. If all the required fields for a hot lead are not null, then we are sending an email to the owner.<\/p>\n\n\n\n<p>When the lead is saved, it goes to the record variable of flow that is \u201cnew Lead,\u201d and then we save it to Lead fields and then proceed to the following process.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/sites\/2\/2020\/05\/edit-get-records.png\" alt=\"edit get records\" class=\"wp-image-2902\"\/><\/figure><\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/multisitelocal.ezxdemo.com\/contact-us.html?utm_source=blog&amp;utm_medium=banner&amp;utm_campaign=emizen_blog\"><img decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/sites\/2\/2020\/05\/hire-salesforce-developers-1.png\" alt=\"hire salesforce developers\" class=\"wp-image-2389\"\/><\/a><\/figure><\/div>\n\n\n<blockquote class=\"wp-block-quote has-text-align-center is-layout-flow wp-block-quote-is-layout-flow\">\n<p>Also Read: <a href=\"https:\/\/multisitelocal.ezxdemo.com\/blog\/embed-lightning-component-in-flow.html\" target=\"_blank\" rel=\"noreferrer noopener\">How To Embed Lightning Component In Flow<\/a><\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Step-by-Step_Guide_with_Code_Examples\"><\/span>Step-by-Step Guide with Code Examples<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 1: Understanding Flows and Apex<\/strong><\/h3>\n\n\n\n<p>Salesforce Flows automate processes visually, while Apex is used for complex logic. Integrating them allows for more dynamic and powerful automation in Salesforce.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: <strong>Creating a Flow<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Navigate to Flow Builder:<\/strong> In Salesforce, go to Setup > Process Automation > Flows.<\/li>\n\n\n\n<li><strong>Create a New Flow:<\/strong> Choose a flow type suitable for your use case, like Record-Triggered or Auto-Launched.<\/li>\n\n\n\n<li><strong>Design the Flow: <\/strong>Add elements such as decisions, assignments, and actions. For instance, create a flow that sends an email when a new lead is added.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Preparing Apex Code<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Open Developer Console:<\/strong> In Salesforce, navigate to the Developer Console.<\/li>\n\n\n\n<li><strong>Create a New Apex Class:<\/strong> Name it, for example, FlowController.<\/li>\n\n\n\n<li><strong>Import Namespaces: <\/strong>Use System.Flow statements to import necessary namespaces.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>public class FlowController {\r\n    \/\/ Class content goes here\r\n}\r<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. Calling the Flow from Apex<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Instantiate the Flow<\/strong>: In your Apex class, instantiate the Flow using <code>System.Flow.Interview<\/code>.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>public class FlowController {\r\n    public void callFlow() {\r\n        \/\/ Set up Lead record as an example\r\n        Lead l = new Lead(FirstName = 'Sapna', LastName = 'Chandani', Email = 'sapna.chandani@example.com', Phone = '8522256355', MobilePhone = '876545465', Company = 'ExampleCompany');\r\n        insert l;\r\n        \r\n        \/\/ Prepare the Flow variables\r\n        Map&lt;String, Object> params = new Map&lt;String, Object>();\r\n        params.put('newLead', l);\r\n        \r\n        \/\/ Instantiate and start the Flow\r\n        Flow.Interview.Your_Flow_Name yourFlow = new Flow.Interview.Your_Flow_Name(params);\r\n        yourFlow.start();\r\n    }\r\n}\r<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Ensure to replace <code>Your_Flow_Name<\/code> with the actual name of your Flow.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">5. Testing and Debugging<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Test the Apex Code<\/strong>: Create Apex Test Classes to ensure the functionality works as expected.<\/li>\n\n\n\n<li><strong>Debug<\/strong>: Utilize System Debug logs and Flow debug tools to troubleshoot issues.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">6. Use Case Example<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Example Scenario<\/strong>: An Apex trigger is set to call a Flow when a lead&#8217;s account status changes, updating contact records accordingly.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">Additional Code Snippets<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Flow Controller Class<\/strong>: The <code>FlowController<\/code> class is already outlined above.<\/li>\n\n\n\n<li><strong>Controller Using the Visualforce Page<\/strong>: Create a Visualforce page to trigger the flow.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;apex:page controller=\"FlowController\">\r\n    &lt;apex:form>\r\n        &lt;apex:commandButton action=\"{!callFlow}\" value=\"Start\" reRender=\"text\"\/>\r\n    &lt;\/apex:form>\r\n&lt;\/apex:page>\r<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Schedulable Class<\/strong>: For automating the flow execution at scheduled intervals.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>global class scheduledFlow implements Schedulable {\r\n    global void execute(SchedulableContext sc) {\r\n        FlowController fc = new FlowController();\r\n        fc.callFlow();\r\n    }\r\n}\r<\/code><\/pre>\n\n\n\n<p>If you are searching for professional <a href=\"https:\/\/multisitelocal.ezxdemo.com\/salesforce-consulting.html\">salesforce consultants<\/a>, contact us.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Also Read: <a href=\"https:\/\/multisitelocal.ezxdemo.com\/blog\/how-to-create-flows.html\" target=\"_blank\" rel=\"noopener\">How To Create A Flow In Salesforce<\/a><\/h4>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Best_Practices_To_Follow_While_Calling_Flows_From_Apex_In_Salesforce\"><\/span>Best Practices To Follow While Calling Flows From Apex In Salesforce<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Ensure robust error handling in both Apex and Flows.<\/li>\n\n\n\n<li>Maintain clear documentation and comments for better readability and maintenance.<\/li>\n\n\n\n<li>Optimize Flow and Apex code for performance.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Troubleshooting_Common_Issues\"><\/span>Troubleshooting Common Issues<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Flow Not Triggering:<\/strong> Check if the Flow is activated and if the input variables in Apex match those in the Flow.<\/li>\n\n\n\n<li><strong>Apex Errors:<\/strong> Validate data types and ensure error handling is in place for null values and exceptions.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Calling flows from Apex in Salesforce is a powerful technique offering developers extensive flexibility and control over their applications. Integrating flows into Apex code allows a <a href=\"https:\/\/multisitelocal.ezxdemo.com\/salesforce.html\">Salesforce development company<\/a> to streamline complex processes, automate tasks, and enhance the overall user experience. <\/p>\n\n\n\n<p>This integration empowers developers to create more dynamic, efficient, customized solutions within the Salesforce platform. Any Salesforce development company needs to master this skill to leverage the full potential of Salesforce, ensuring its clients receive the most advanced and efficient solutions possible. Remember, the key to success in Salesforce lies in continually evolving with its versatile features.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>After the release of Summer &#8217;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 the flow: interview component, but in this case, we need to use UI and User Interaction<\/p>\n","protected":false},"author":39,"featured_media":40472,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"MSN_Categories":"Uncategorized","MSN_Publish_Option":false,"MSN_Is_Local_News":false,"MSN_Is_AIAC_Included":"Empty","MSN_Location":"[]","MSN_Add_Feature_Img_On_Top_Of_Post":false,"MSN_Has_Custom_Author":false,"MSN_Custom_Author":"","MSN_Has_Custom_Canonical_Url":false,"MSN_Custom_Canonical_Url":"","_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[87],"tags":[],"class_list":{"0":"post-2899","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-salesforce-development"},"modified_by":"Marketing EmizenTech","featured_image_src":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-content\/uploads\/sites\/2\/2020\/05\/How-To-Call-Flows-From-Apex-In-Salesforce-600x400.jpg","featured_image_src_square":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-content\/uploads\/sites\/2\/2020\/05\/How-To-Call-Flows-From-Apex-In-Salesforce-600x408.jpg","author_info":{"display_name":"Virendra Sharma","author_link":"https:\/\/multisitelocal.ezxdemo.com\/blog\/author\/salesforce"},"_links":{"self":[{"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/posts\/2899","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/comments?post=2899"}],"version-history":[{"count":0,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/posts\/2899\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/media\/40472"}],"wp:attachment":[{"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/media?parent=2899"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/categories?post=2899"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/tags?post=2899"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}