{"id":6501,"date":"2021-12-30T06:50:41","date_gmt":"2021-12-30T06:50:41","guid":{"rendered":"https:\/\/www.greytrix.com\/blogs\/salesforce\/?p=6501"},"modified":"2024-05-23T11:46:56","modified_gmt":"2024-05-23T11:46:56","slug":"how-to-call-an-apex-class-from-a-screen-flow","status":"publish","type":"post","link":"https:\/\/www.greytrix.com\/blogs\/salesforce\/2021\/12\/30\/how-to-call-an-apex-class-from-a-screen-flow\/","title":{"rendered":"How to call an APEX Class from a Screen-Flow"},"content":{"rendered":"\n<p>This blog post will go over how to use a Screen Flow to call an Apex class using a straightforward example. The Screen Flow generated in this example has three variables that we have created. Initially, recordId is configured to be available for both input and output.<\/p>\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-link-color wp-elements-c2c282b1dc1f37ae0dff6e159ab7edab\"><strong>1. RecordId Variable<\/strong><\/p>\n\n\n\n<center><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2021\/12\/1.RecordId-Variable.jpg\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" class=\"size-full\" style=\"border: 1px solid #A9A9A9; padding: 2px; margin: 2px; align: center;\" src=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2021\/12\/1.RecordId-Variable.jpg\" alt=\"RecordId Variable\"><\/a><\/center>\n\n\n\n\n<p>The second variable that we have created is the <strong>Account <\/strong>to store Account Data.<\/p>\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-link-color wp-elements-068a54b47f768dc8fd1c20c8d233481a\"><strong>2. Account Variable<\/strong><\/p>\n\n\n\n<center><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2021\/12\/2.Account-Variable.jpg\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" class=\"size-full\" style=\"border: 1px solid #A9A9A9; padding: 2px; margin: 2px; align: center;\" src=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2021\/12\/2.Account-Variable.jpg\" alt=\"Account Variable\"><\/a><\/center>\n\n\n\n\n<p>The third variable that we have created is <strong>Name<\/strong>. That stores the return value of the Apex class called in the flow.<\/p>\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-link-color wp-elements-5734c9c5d4c7c2abbd18036286fc50f7\"><strong>3. Name Variable<\/strong><\/p>\n\n\n\n<center><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2021\/12\/3.Name-Variable.jpg\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" class=\"size-full\" style=\"border: 1px solid #A9A9A9; padding: 2px; margin: 2px; align: center;\" src=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2021\/12\/3.Name-Variable.jpg\" alt=\"Name Variable\"><\/a><\/center>\n\n\n\n\n<p>Then we create an Invocable method in class <strong>InvokeAccountAction.apxc <\/strong>which will be called through the Screenflow to return the <strong>Name <\/strong>of an particular Account as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code has-background\" style=\"background-color:#e6e6e6\"><code>public class InvokeAccountAction \n{\n  @InvocableMethod(callout=true label='Get Account Name')    \n  public static List&lt;String&gt; getAccountNames(List&lt;ID&gt; ids) {\n    List&lt;String&gt; accountNames = new List&lt;String&gt;();\n    List&lt;Account&gt; accounts = &#91;SELECT Name FROM Account WHERE Id in :ids];\n    for (Account account : accounts) {\n      accountNames.add(account.Name);\n    }\n    return accountNames;\n  } \n}\n<\/code><\/pre>\n\n\n\n<p>Meanwhile, to call the Apex class from the flow we create an <strong>Apex action <\/strong>in the flow as shown in the image below:<\/p>\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-link-color wp-elements-767a305fcb2ae0d607346b39600b4751\"><strong>4. Apex Action<\/strong><\/p>\n\n\n\n<center><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2021\/12\/4.Apex-Action.jpg\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" class=\"size-full\" style=\"border: 1px solid #A9A9A9; padding: 2px; margin: 2px; align: center;\" src=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2021\/12\/4.Apex-Action.jpg\" alt=\"Apex Action\"><\/a><\/center>\n<font size=\"2\"><center><i>Apex Action<\/i><\/center><\/font>\n\n\n\n<p>For example,\u00a0after calling an Apex class we want to make sure that <strong>the return value <\/strong>against the field <strong>Name <\/strong>of the particular Account is not empty.<\/p>\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-link-color wp-elements-f4355db9708fcb9f76cfa47c0292ba77\"><strong>5. Check Return Value<\/strong><\/p>\n\n\n\n<center><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2021\/12\/5.Check-return-value.jpg\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" class=\"size-full\" style=\"border: 1px solid #A9A9A9; padding: 2px; margin: 2px; align: center;\" src=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2021\/12\/5.Check-return-value.jpg\" alt=\"Check return value\"><\/a><\/center>\n\n\n\n\n<p>After that, we create a screen to show the name of that Account, if the condition is satisfied.<\/p>\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-link-color wp-elements-b76a62e337929bfeb3e1036d232e6c96\"><strong>6. Edit Screen<\/strong><\/p>\n\n\n\n<center><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2021\/12\/6.Screen.jpg\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" class=\"size-full\" style=\"border: 1px solid #A9A9A9; padding: 2px; margin: 2px; align: center;\" src=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2021\/12\/6.Screen.jpg\" alt=\"Edit Screen\"><\/a><\/center>\n<font size=\"2\"><center><i>Edit Screen<\/i><\/center><\/font>\n\n\n\n<p>Likewise, the whole flow can be built as shown in the image below:<\/p>\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-link-color wp-elements-5041ec8ba0e18b2b617ceae1dcb53a3a\"><strong>7. ScreenFlow<\/strong><\/p>\n\n\n\n<center><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2021\/12\/7.ScreenFlow.jpg\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" class=\"size-full\" style=\"border: 1px solid #A9A9A9; padding: 2px; margin: 2px; align: center;\" src=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2021\/12\/7.ScreenFlow.jpg\" alt=\"ScreenFlow\"><\/a><\/center>\n\n\n\n\n<p>Likewise, after you have completed built the flow you can create a Button <strong>Invoke Method <\/strong>to call the flow:<\/p>\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-link-color wp-elements-ad6f96748136a9cf86d7ae956261f6d6\"><strong>8. Account Screen with Invoke Method Button<\/strong><\/p>\n\n\n\n<center><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2021\/12\/8.AccountScreen.jpg\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" class=\"size-full\" style=\"border: 1px solid #A9A9A9; padding: 2px; margin: 2px; align: center;\" src=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2021\/12\/8.AccountScreen.jpg\" alt=\"Account Screen with Invoke Method Button\"><\/a><\/center>\n\n\n\n\n<p>After execution of the flow, we get a Screen which shows the Account Name as shown below:<\/p>\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-link-color wp-elements-4902f3a965c0c10a5025c7f57acd51b7\"><strong>9. Resulted Data<\/strong><\/p>\n\n\n\n<center><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2021\/12\/9.Result.jpg\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" class=\"size-full\" style=\"border: 1px solid #A9A9A9; padding: 2px; margin: 2px; align: center;\" src=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2021\/12\/9.Result.jpg\" alt=\"Resulted Data\"><\/a><\/center>\n\n\n\n\n<p>We hope that you find this blog helpful, if you still have queries, don\u2019t hesitate to contact us at <a rel=\"noreferrer noopener\" href=\"mailto:salesforce@greytrix.com\" target=\"_blank\">salesforce@greytrix.com<\/a>.<\/p>\n\n\n\n<p style=\"text-align: justify\"><b>About Us<\/b><\/br>\n<p><a href=\"https:\/\/www.greytrix.com\/\">Greytrix<\/a> \u2013 a globally recognized and one of the oldest Sage Development Partner and a Salesforce Product development partner offers a wide variety of integration products and services to the end users as well as to the Partners and Sage PSG across the globe. We offer Consultation, Configuration, Training and support services in out-of-the-box functionality as well as customizations to incorporate custom business rules and functionalities that require apex code incorporation into the Salesforce platform.<br><br> Greytrix has some unique solutions for Cloud CRM such as <a href=\"\">Salesforce Sage integration<\/a> for <a href=\"https:\/\/www.greytrix.com\/sage-x3-erp\/integration\/\">Sage X3<\/a>, <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/sage-100-integration\/\">Sage 100<\/a> and <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/sage-300-integration\/\">Sage 300 (Sage Accpac)<\/a>. We also offer best-in-class Cloud CRM <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/crm-development\/\">Salesforce customization and development services<\/a> along with services such as Salesforce <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/data-migration-support\/\">Data Migration<\/a>, <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/crm-development\/\">Integrated App development<\/a>, Custom App development and Technical Support business partners and end users. Salesforce Cloud CRM integration offered by Greytrix works with Lightning web components and supports standard opportunity workflow. Greytrix GUMU&#x2122; integration for Sage ERP \u2013 Salesforce is a 5-star rated app listed on <a href=\"https:\/\/appexchange.salesforce.com\/appxListingDetail?listingId=a0N30000000psM5EAI\" target=\"_blank\" rel=\"noopener\">Salesforce AppExchange<\/a>.<br> The GUMU&#x2122; Cloud framework by Greytrix forms the backbone of cloud integrations that are managed in real-time for processing and execution of application programs at the click of a button.<br><br> For more information on our Salesforce products and services, contact us at <a href=\"mailto:salesforce@greytrix.com\">salesforce@greytrix.com<\/a>. We will be glad to assist you.<\/p>\n\n\n\n<p><strong>Related Posts<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2021\/09\/22\/how-to-use-email-template-in-apex-code\/\" target=\"_blank\">How to Use Email Template in Apex Code<\/a><\/li>\n\n\n\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2021\/08\/17\/how-to-create-a-dynamic-multi-filter-object-in-salesforce-part-i\/\" target=\"_blank\">Create a dynamic multi-filter object in Salesforce-Part I<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2021\/12\/06\/how-to-invoke-lightning-auto-launched-flow-from-the-apex-class\/\" data-type=\"URL\" data-id=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2021\/12\/06\/how-to-invoke-lightning-auto-launched-flow-from-the-apex-class\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to invoke lightning Auto launched flow from Apex class<\/a><\/li>\n<\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This blog post will go over how to use a Screen Flow to call an Apex class using a straightforward example. The Screen Flow generated in this example has three variables that we have created. Initially, recordId is configured to be available for both input and output. 1. RecordId Variable The second variable that we\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2021\/12\/30\/how-to-call-an-apex-class-from-a-screen-flow\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[13,859,14,15,16,18,1240,1238,1237,1235,1239,1233,1234,1236,1241],"class_list":["post-6501","post","type-post","status-publish","format-standard","hentry","category-salesforce-srv","tag-apex","tag-apex-class","tag-apex-classes","tag-apex-development","tag-apex-in-salesforce","tag-apex-methods","tag-invocable-actions","tag-invocable-apex","tag-invocable-apex-example","tag-invocable-apex-from-flow","tag-invocable-class-syntax","tag-invocable-method","tag-invoke-apex-class","tag-invoke-method","tag-screenflow"],"_links":{"self":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts\/6501","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/comments?post=6501"}],"version-history":[{"count":4,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts\/6501\/revisions"}],"predecessor-version":[{"id":8429,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts\/6501\/revisions\/8429"}],"wp:attachment":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/media?parent=6501"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/categories?post=6501"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/tags?post=6501"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}