In this approach we use Salesforce Apex invocable method to make an API call to Salesforce Data Cloud and fetch back the data in a Flow. As an example, a Screen Flow can be utilized to display fetched data. The data fetched from Data Cloud can be utilized in other ways – e.g., trigger processes or insert/update other records in the connected Salesforce CRM Org.

There are two steps in this approach.
(1)Establish authentication from your core Salesforce CRM Org to Salesforce Data Cloud
(2)Fetch data using Apex callout (Apex Invocable Method) from a Flow.

To establish authentication between your core Salesforce Org and the Data Cloud, you will first create a connected app in the Data Cloud Home Org. You will also need to create a permission set, assign permission set to Connected app, assign default data space to the permission set. The connected app takes a few minutes to activate.

After creating the connected app in Data Cloud home org, you would need to create an Auth Provider, External Credential and a Named Credential in the connected core CRM Org. You may also want to create a Remote Site setting in the connected core CRM Org.

This post will not get in the details of connected app creation or Named Credentials There are a lot of excellent YouTube videos and articles on the internet which explain the step by step creation of Connected Apps, Named Credentials.

Now comes the fun part of writing apex code to fetch data using the Connected app. For this the first thing is to review the Salesforce Data Cloud API specifications for retrieving data. This API is also called Data Cloud Connect API and we shall be specifically using query API.

We shall be sending a post request with a select SQL statement to retrieve data and then we shall process the response in our Apex.

It’s important to examine the response JSON structure as specified in the query api documentation. This determines how you will process the response in Apex.

—————–

public class FlowDataRetriever {
@InvocableMethod(label='Fetch Data from External API' description='Fetches data from an external system using a SQL query.')
public static List fetchData(List inputs) {
// Flattened list to return directly to Flow
List results = new List();

for (FlowInput input : inputs) {
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('callout:afins_dc_jul_2024_nc/services/data/v61.0/ssot/queryv2');
request.setTimeout(120000);
request.setMethod('POST');
request.setHeader('Content-Type', 'application/json;charset=UTF-8');

// SQL query
String query = 'SELECT UnifiedssotIndividualI01__dlm.ssot__FirstName__c FirstName__c, ' +
'UnifiedssotIndividualI01__dlm.ssot__LastName__c LastName__c, ' +
'UnifiedssotIndividualI01__dlm.ssot__Id__c as UID__c, ' +
'UnifiedssotIndividualI01__dlm.ssot__BirthDate__c as BD__c ' +
'FROM UnifiedssotIndividualI01__dlm ' +
'JOIN UnifiedLinkssotIndividualI01__dlm ON UnifiedssotIndividualI01__dlm.ssot__Id__c = UnifiedLinkssotIndividualI01__dlm.UnifiedRecordId__c ' +
'JOIN ssot__Individual__dlm ON ssot__Individual__dlm.ssot__Id__c = UnifiedLinkssotIndividualI01__dlm.SourceRecordId__c ' +
'WHERE ssot__Individual__dlm.ssot__Id__c = \'' + String.escapeSingleQuotes(input.recordId) + '\'';

request.setBody('{"sql" : "' + query + '"}');

try {
HttpResponse response = http.send(request);
System.debug('Response Status: ' + response.getStatusCode());
System.debug('Response Body: ' + response.getBody());

if (response.getStatusCode() == 200 || response.getStatusCode() == 201) {
Map responseMap = (Map) JSON.deserializeUntyped(response.getBody());

if (responseMap.containsKey('data')) {
List dataList = (List) responseMap.get('data');

for (Object rowObj : dataList) {
if (rowObj instanceof List) {
List rowData = (List) rowObj;

// Create FlowOutput object
FlowOutput output = new FlowOutput();
output.firstName = (rowData.size() > 0) ? (String) rowData[0] : null;
output.lastName = (rowData.size() > 1) ? (String) rowData[1] : null;
output.uid = (rowData.size() > 2) ? (String) rowData[2] : null;
output.birthDate = (rowData.size() > 3) ? (String) rowData[3] : null;

results.add(output);
}
}
}
} else {
System.debug('Error: Received non-success status code.');
}
} catch (Exception e) {
System.debug('Error: ' + e.getMessage());
}
}

System.debug('Results returned to Flow: ' + results);
return results; // Return directly as a flattened list
}

public class FlowInput {
@InvocableVariable(required=true label='Record ID' description='Record ID to filter the query.')
public String recordId;
}

public class FlowOutput {
@InvocableVariable(label='First Name' description='First name of the individual.')
public String firstName;

@InvocableVariable(label='Last Name' description='Last name of the individual.')
public String lastName;

@InvocableVariable(label='UID' description='Unique ID of the individual.')
public String uid;

@InvocableVariable(label='Birth Date' description='Birth date of the individual.')
public String birthDate;
}
}

——————–

Once you create this Apex code in your Salesforce Org, the methods will become available in Flow Apex actions. You will need to create an Input resource variable in flow that will accept the record variable. You will then need to map the apex where clause variable to this flow variable.

You can embed the flow in a contact record page and pass the record variable to the flow variable which in turn gets passed to the SQL statement where clause in Apex.

I have not gone into details of Flow as my goal was to show a working example of an APEX code that makes an API call to Data Cloud.

This is for demo purpose only. Please use the official Salesforce documentation for your project.

Additional Reference: A Practical Example of Apex Callout to Salesforce Data Cloud API for Displaying Data in Salesforce LWC (Lightning Web Component)

For additional reading here is an excellent post on API Callout to Data Cloud by Arastun Effendiyev

 2,481 total views,  11 today

© 2025 appcloud101.com. All rights reserved.