HTTP Callout from Flow - Part 2 - Connecting 2 Salesforce orgs

This blog is a continuation of my last blog in which we had a look at how to call a public REST API from a screen flow. Many of you were interested to know about how will this work out if the REST API needs authentication. How can we handle the authentication in the flow. Another use case which came up was if we can connect 2 Salesforce Org using this approach. So today, lets have a look at how we can establish a connection between 2 Salesforce Orgs.

For this example, lets call one org as the Data Producer and another org as the Data Consumer.

Data Producer Org: As the name suggests, this org will be responsible for providing the data. This org will host a REST webservice. We will also create a connected app in this org using which any external system can access this webservice. In our case, the external system will be another Salesforce org which is the Data Consumer Org. In your case, it can be any other system as well.

Step 1: Create a REST Webservice in the Data Producer Org


@RestResource(urlMapping='/contactLookup/*')
global with sharing class ContactLookupService {
    
    @HttpGet
    global static ContactLookupResponse getRandomContact() {
        Contact contact = [SELECT Id,Name FROM Contact LIMIT 1];
        if (contact != null) {
            return new ContactLookupResponse(contact.Name, contact.Id);
        }
        return null;
    }
    
    global class ContactLookupResponse {
        global String name {get;set;}
        global String recordId {get;set;}
        
        ContactLookupResponse(String name, String recordId){
            this.name = name;
            this.recordId = recordId;
        }
    }
}

The webservice does not have any complex logic. It just returns you the name and record id of a random contact from the org.

Step 2: Create a Connected App in the Data Producer Org

Using this connected app, any external application will be able to consume the webservice that we created in Step 1.

Navigate to Setup>App Manager>New Connected App

Provide a meaningful name. Under API (Enable OAuth settings), check the Enable OAuth Setting option. In the callback url, provide any valid url for now. We will come back and change this url once we created the Auth Provider in the Data Consumer Org. In the Selected OAuth scopes, select the following scopes: "Manage user data via APIs (api)" and "Perform requests at any time (refresh_token, offline_access)". Check the "Require Secret for Web Server Flow" option. Leave rest of the options as default and click on Save.

 

We are done with the setup required in the Data Producer Org. Now lets have a look at the steps required in the Data Consumer Org.

Data Consumer Org: As the name suggests, this org will consume the data from the Data Producer Org. In this org, we will create an Auth Provider, a Named Credential and a Screen Flow. The named credential will be consumed by the screen flow. The Auth Provider will be consumed by the named credential. The screen flow will make the HTTP callout to the REST webservice present in the Data Producer org. 

Step 3: Create an Auth Provider in the Data Consumer Org

Navigate to Setup> Auth Provider > New. Provide meaningful name. Copy the Consumer Key and Consumer Secret from the connected app created in Data Provider Org in Step 2. In the Default Scopes, enter the following value (without double quotes): "refresh_token api. Leave rest of the values as default and click on Save.

 

The following page will open. From this page, copy the Callback URL. Open the Connected App which you created in the Data Producer Org and replace the original Callback URL with this value.

 


Step 4: Create a Named Credential in the Data Producer Org 

Navigate to Setup> Named Credentials > New (Legacy). Provide a meaningful bame. In the URL, provide the URL of the Data Producer Org since this is the org with which we want to connect. Under Identity Type, select Named Principal. Under Authentication Protocol, select OAuth 2.0. Under Authentication Provider, select the Auth Provider which we created in Step 3. Select the following options: "Start Authentication Flow on Save", "Generate Authorization Header" and "Allow Merge Fields in HTTP Body" and click on Save. The authentication page will open where you need to enter the username and password of the Data Producer Org.

 Step 5: Create a screen flow in the Data Producer Org

Navigate to Setup>Flows>New and select Screen Flow. Add an Action element and select Create HTTP Callout. Give a meaningful name and select the named credential which we created in Step 4 and click on Next.

 

Provide a meaningful name for the Invocable Action. Select the method as GET. In the URL Path, enter the following value (without double quotes): "/services/apexrest/contactLookup". Under Provide Sample Response section, click on New and enter the following value in the Sample Json Response:


{
  "recordId" : "0035i00001140JyAAI",
  "name" : "Rose Gonzalez"
}

You can get this value by calling the REST webservice from the workbench and copy the raw response. Click on Review. Salesforce will generate the required data structure. Click on Done and then click on Save.

 

 Provide a meaningful name to the external service and click on Done. Save the flow. You can add a screen element to display the response. On detailed steps on how to do that, please refer my previous blog. In this example, we will just debug the flow and check the API response in the Debug mode. Click on Debug and you would be able to see the magic in action.


 If you have any questions, please leave a comment. Connect with me on LinkedIn for more of such exciting content.

Comments

Popular posts from this blog

Salesforce Hacks: System.LimitException: Too many queueable jobs added to the queue: 2

Salesforce Lightning: Countdown timer

Building an Org Role Hierarchy component in LWC