Salesforce has advanced programming by introducing Flows, which allows users to configure complex flows in a matter of minutes. However, comfort has its drawbacks, as there are some limitations to what a Flow can do.
In this blog, we'll walk through a simple example of how to call an Apex class from a Screen Flow. In this example, we've created a Screen Flow with two variables, one is recordId, set as available for Input and another one is OutputFromApex.
In this example we are creating a screen flow. Then we have created a quick action in the Account object and then in this quick action we call our screen flow. Now because we have created a variable i.e recordId in screen flow. This variable will pick up the recordId from the account record. Now what we are trying to achieve from this screen flow is that, if the count of contacts associated with the account record is zero, then we should show on the screen of flow that "No contacts were Found" and if it founds, then show on the screen of flow that the "Contacts were Found". For this we will give this recordId as an input parameter in our Apex Action. Then our Apex will find the List of Contacts on the basis of this recordId. If the size of the list of contacts is more than zero, then we will return 'Contact Found' to our flow else if it not found then we will return 'Not Found' and display this message in our Screen Flow.
From Setup, in the Quick Find box, enter Flows, select Flows, and then click New Flow.
Select the flow type as "Screen Flow", then click Next.
Click the "New Resource" button in the Flow builder. Select Resource Type as 'Variable'. Give the api Name as 'recordId'. Select data type as 'Text'. In the Availability Outside the Flow options selects the checkbox 'Available for Input'.
Create one more variable "OutputFromApex". Select data type as 'Text' to store the output that we are getting from apex.
Save the flow as "Call an APEX Class from a Screen-Flow".
Now we need to create an Invocable method in the Apex class i.e. InvokeFlowAction.apxc that will be called through the Screen flow to return the status Contact Found or Not for a specific account record, as follows:
public class InvokeFlowAction {
public class FlowParams{
@InvocableVariable(required=true)
public string acctId;
}
@InvocableMethod(callout=true label='Get Account RecordId Through Flow')
public static List<String> passQuoteData(FlowParams[] params)
{
List<string> strList=new List<string>();
List<contact> conList=[Select Id from Contact where AccountId=:params.get(0).acctId];
if(conList.size()>0)
{
strList.add('Contact Found');
}
else
{
strList.add('Contact Not Found');
}
return strList;
}
}
Meanwhile, to call the Apex class from the flow, we have to add an Apex action to our as shown in the image below:
From the flow Interaction, click on Action:
After this from the Apex Action search for your action by the label that we have provided in the apex class and once found click on that Action.
After this give the label as you want and set the input parameter i.e. acctId value and return value that we are getting from apex to the variables that we have created in the flow.
The Next Step is to add the Decision element to decide what output we are getting from our Apex class.
Now Add the Screen to display the information Contact found or Not Found to the User.
Note: We are using "OutputFromApex" variable as a resource to display the message. Because this variable is holding the output that we are getting from Apex.
Screen: If Contact Found-
Screen: If Contact Not Found-
Next Step is to Activate this Flow. Now we are almost done with are our work, Only one step is left that is Creating a Quick action on Account Object and calling our flow from this quick action and add this quick action to account page layout.
Now we are ready to test our functionality:
We hope that you find this blog helpful, if you still have queries, don’t hesitate to contact us at omsfdc91@gmail.com.
No comments:
Post a Comment