Module sfdc
ballerinax/sfdc
Overview
The Salesforce connector allows users to perform CRUD operations for SObjects, query using SOQL, search using SOSL, and describe SObjects and organizational data through the Salesforce REST API. Apart from these functionalities, Ballerina Salesforce Connector includes a listener to capture events.
This module supports Salesforce v48.0 REST API.
Prerequisites
Before using this connector in your Ballerina application, complete the following:
- Create Salesforce account
- Obtain tokens
- Client tokens - Follow the steps listed under OAuth 2.0 Web Server Flow for Web App Integration.
- Listener tokens - Follow the steps listed under Reset Your Security Token generate secret key and follow the steps listed under Subscription Channels to subscribe channels.
Quickstart
To use the Salesforce connector in your Ballerina application, update the .bal file as follows:
Client
Step 1: Import connector
Import the ballerinax/sfdc
module into the Ballerina project.
import ballerinax/sfdc;
Step 2: Create a new connector instance
Create a sfdc:SalesforceConfiguration
with the OAuth2 tokens obtained, and initialize the connector with it.
sfdc:SalesforceConfiguration sfConfig = { baseUrl: <"EP_URL">, clientConfig: { clientId: <"CLIENT_ID">, clientSecret: <"CLIENT_SECRET">, refreshToken: <"REFRESH_TOKEN">, refreshUrl: <"REFRESH_URL"> } }; sfdc:Client baseClient = new(sfConfig);
Step 3: Invoke connector operation
-
Now you can use the operations available within the connector. Note that they are in the form of remote operations.
Following is an example on how to create a record using the connector.json accountRecord = { Name: "John Keells Holdings", BillingCity: "Colombo 3" }; public function main() returns error? { string recordId = check baseClient->createRecord("Account", accountRecord); }
-
Use
bal run
command to compile and run the Ballerina program.
Listener
Step 1: Import connector
Import the ballerinax/sfdc
module into the Ballerina project.
import ballerinax/sfdc;
Step 2: Create a new connector listener instance
Create a sfdc:ListenerConfiguration
with the basic credentials obtained, and initialize the connector with it.
The password should be the concatenation of the user's Salesforce password and secret key.
sfdc:ListenerConfiguration listenerConfig = { username: config:getAsString("SF_USERNAME"), password: config:getAsString("SF_PASSWORD") }; listener sfdc:Listener eventListener = new (listenerConfig);
Step 3: Invoke listener service
-
Now you can use the channel available in the Salesforce and capture the events occurred.
Following is an example on how to capture all events using the connector.@sfdc:ServiceConfig { channelName:"/data/ChangeEvents" } service /quoteUpdate on eventListener { remote function onUpdate (sfdc:EventData quoteUpdate) { json quote = quoteUpdate.changedData.get("Status"); } }
-
Use
bal run
command to compile and run the Ballerina program.