Salesforce to S/4 HANA Integration with Ballerina: Part 1

Niveathika Rajendran
Ballerina Swan Lake Tech Blog
3 min readJul 16, 2024

This article was written using Ballerina Swan Lake Update 9.0 (2201.9.0).

Why Ballerina for Integration?

Ballerina is a programming language designed specifically for integration. It features unique abstractions that incorporate network aspects directly into the language, making network services, endpoints, and actions first-class citizens. This makes it easy to write integration scenarios in Ballerina.

We will automate the creation of sales orders in S/4HANA when an opportunity is closed with Won state in Salesforce.

In this first part of our two-part series, we’ll explore how to build a Salesforce listener using Ballerina. By the end of this post, you’ll have a functional Salesforce listener that captures opportunity changes and extracts data to identify events with the “Closed Won” state.

Prerequisites

Before we dive into the code, make sure you have the following prerequisites:

  1. Ballerina: Installed on your system. You can download it from ballerina.io.
  2. Salesforce Account: Access to Salesforce with necessary API permissions.
  3. Subscribe to Salesforce Change Capture Event

Step 1: Initialise a Ballerina Project

Create a new Ballerina project using the following commands:

bal new salesforce-to-sap
cd salesforce-to-sap

Note: The ‘main.bal’ will contain a sample “Hello World” program. You can delete it and get started on the project.

Step 2: Import Salesforce Listener Module and Initialise the Listener

We will use the ‘ballerinax/trigger.salesforce’ package to implement the listener. As the first step, initialise the listener.

Optional: Store Credentials Securely

In Ballerina, credentials can be stored in ‘Config.toml’ for security. Ballerina will load the credentials at runtime.

The channel name for capturing opportunity object changes is ‘/data/OpportunityChangeEvent’.

This Salesforce documentation elaborates more on how these channel names are derived.

Step 3: Attach salesforce:RecordService to Salesforce Listener

Now that we have initialised the listener, we need to access the changed event. Attach a salesforce:RecordService to the listener. A service represents a collection of remotely accessible methods attached to a particular listener. Here, the RecordService has functions designed for Salesforce events.

Step 4: Implement the onUpdate Function to Filter Opportunity Close Won Events

For this integration, we are only interested in the onUpdate function. Let’s see how it can be implemented to filter only opportunity won events.

Step 5: Retrieve Opportunity Items

So far, we have captured specific events. However, this event does not include all the items the opportunity contains. These are not included because this will be finalized beforehand. The OpportunityLineItem table in Salesforce keeps track of all products added to an opportunity.

Use the following SOQL query to retrieve all product details in the opportunity that was closed:

SELECT ProductCode, Name, Quantity 
FROM OpportunityLineItem
WHERE OpportunityId=<id>;

Ballerina’s ‘ballerinax/salesforce’ package provides the client to run the above query.

Step 6: Initialise Salesforce Client

The Salesforce Setup Guide in Ballerina documentation provides detailed steps to obtain the required tokens.

Step 7: Execute the SOQL Query

Results will be bound to the SfOpportunityItem Ballerina record for easy manipulation.

Full Code So Far

Here’s the complete code up to now,

Conclusion

In this part, we’ve set up a Salesforce listener using Ballerina that listens for opportunity change events and retrieves the necessary opportunity items. We’ve covered how to configure the listener and client, handle events, and query Salesforce for data.

In part 2, we’ll explore how to use the Ballerina S/4 HANA Sales Order Connector to create a sales order from the extracted details.

Feel free to leave your comments and questions below!

--

--