The Salesforce connector allows you to perform CRUD operations for SObjects, query using SOQL, search using SOSL, and describe SObjects and organizational data through the Salesforce REST API. It handles OAuth 2.0 authentication.
Ballerina Version | API Version |
---|---|
0.975.0 | v37.0 |
-
Refer to Getting Started guide to download and install Ballerina.
-
Create a Salesforce account and create a connected app by visiting Salesforce and obtain the following parameters:
- Base URl (Endpoint)
- Client Id
- Client Secret
- Access Token
- Refresh Token
- Refresh URL
IMPORTANT: This access token and refresh token can be used to make API requests on your own account's behalf. Do not share your access token, client secret with anyone.
Visit here for more information on obtaining OAuth2 credentials.
-
Create a new Ballerina project by executing the following command.
<PROJECT_ROOT_DIRECTORY>$ ballerina init
-
Working with Salesforce REST connector.
All the actions return JSON or sfdc37:SalesforceConnectorError. If the action is a success, then result (non-empty) JSON will be returned while the sfdc37:SalesforceConnectorError will be null and vice-versa.
You can import the Salesforce package(sfdc37) to your Ballerina program as follows.
import wso2/sfdc37;
- Request
import wso2/sfdc37 as sf;
import ballerina/io;
//User credentials to access Salesforce API
string url = "<base_url>";
string accessToken = "<access_token>";
string refreshToken = "<refresh_token>";
string clientId = "<client_id>";
string clientSecret = "<client_secret>";
string refreshUrl = "<refreshUrl>";
public function main (string... args) {
endpoint Client salesforceClient {
clientConfig:{
url:url,
auth:{
scheme:http:OAUTH2,
accessToken:accessToken,
refreshToken:refreshToken,
clientId:clientId,
clientSecret:clientSecret,
refreshUrl:refreshUrl
}
}
};
//Call the Salesforce connector function getAvailableApiVersions().
json|sf:SalesforceConnectorError response = salesforceClient -> getAvailableApiVersions();
match response {
//if successful, returns JSON result
json jsonRes => {
io:println(jsonRes);
}
//if unsuccessful, returns an error of type sfdc37:SalesforceConnectorError
sf:SalesforceConnectorError err => {
io:println(err);
}
}
}
- Response
JSON response or SalesforceConnectorError struct