Today, we will take a look at one of the most common use cases, i.e., making an HTTP callout from the screen flow. In the traditional approach, we need to create an Apex controller that will have an invocable method. This invocable method needs to be called from the flow using the Apex action element. The Apex controller will handle the callout and return the response to the screen flow.
In the Spring '23 release, Salesforce introduced a capability that allows us to make HTTP callouts directly from the flow without writing a single line of code. In this post, we will explore how to make a GET API call from the flow.
Step 1 - Create a Named Credential:
The first step is to create a named credential. Since the Cat Fact API does not require any authentication, creating the named credential is very simple.
Navigate to Setup > Named Credentials. Under the Named Credentials tab, click on the arrow next to the New button, and select 'New Legacy'. Populate the values as shown in the screenshot below:
Step 2 - Create a Flow:
The next step is to create a flow. Navigate to Setup > Flows > New Flow and choose 'Screen Flow'. Click on 'Add Element' and then 'Add an Action'. You will see an option named "Create HTTP Callout"
Step 3 - Create an External Service:
The next step is to create an external service. Provide a meaningful name in the 'Name' field, then select the Named Credential we created in Step 1 and click the 'Next' button.
Step 4 - Configure Invocable Action: The next step is to configure the invocable action. You can think of this step as equivalent to creating an invocable method in the apex controller. Provide a meaningful name in the Label and select the method as GET. Since we do not need any URL parameters for this API, we will leave it blank. Additionally, we do not require any query parameters here, so we will leave that field blank as well.
Using this information, the flow now knows which endpoint needs to be called and which method need to be invoked. But what about the response? The next step is to let the flow know about the response structure of the API so that it can generate the required classes in the background. Scroll down to the same window and provide the sample API response and click on Review. The flow will identify the data type of the attributes automatically. You also get an option to change the data type if you think that the one identified by the flow is not correct. Click on Done and then click on Save.
Step 5 - Create an Action - The last step is to create an action which will invoke the external service.
Voila! Your flow is ready. Click on Save and let's see the flow in action. Click on the Debug button to see the output. In the below image, you can see that the output is stored in the 2xx variable.
Lets add a screen element in our flow to visualise the output. The output of the API will be available under the Actions which we created in the last step.
Here is the final outcome:
You can see that Salesforce has made it so simple to do an HTTP callout from flows now. You dont need to write a single line of code to do this. Please give it a try and if you have any questions, please post them in the comments section.
Comments
Post a Comment