SIL and Salesforce

In this article I will talk about how you can work with Salesforce from SIL.

SIL is a programming language for automating actions in Atlassian Jira and Confluence. You can learn more about SIL  here .

There are 3 options for working with Salesforce from SIL:

  1. Use the Power Salesforce Connector plugin  .
  2. Write your functions on SIL.
  3. Write your extension for SIL .

In this article, we will look at the first two options. You can read about how to create your own extensions for SIL here .

But before moving on to the code, you must configure Salesforce.

Setting up Salesforce


We will use the Salesforce Rest API to work with Salesforce, so first we need to configure the connected app.
In order to configure the plug-in application, go to gear -> Setup:



Choose App -> App Manager:



Click on the New Connected App button:



Select the option to enable OAuth (Enable OAuth settings). Install  Full Access , click on the Add button  and then on the Save button  .

On the next page, under OAuth settings, we see the Consumer Key and the option to open the Consumer Secret. Remember these two parameters. We will need them to establish a connection with Salesforce.

In addition, you need to generate a user secret token. To do this, go to User Settings → Reset My Security Token. A new secret user key will be sent to our e-mail:



Now we have everything we need to work with Salesforce from SIL.

Power salesforce connector


Power Salesforce connector allows you to work with such objects in Salesforce as Account and Opportunity, in addition, you can receive query results in SOQL. You can find all the functions available in the SIL plugin here .

The advantage of using the plugin is that you do not need to write your SIL functions to work with Salesforce and understand the intricacies of the Salesforce Rest API. This will allow you to concentrate on business tasks, and not on technical tasks.

But of course, the plugin also has a number of disadvantages:



So, let's say we decided to use the Power Salesforce connector.

First we need to install this plugin from the Atlassian Marketplace , and then create a connection to Salesforce from Atlassian Jira (gear -> manage apps -> SFDC Connection Configuration):



Click the Add connection button:



Fill out the required fields and click the Save button. When entering the password, you must enter the user password and then the user's secret key.

Now we can write code in SIL.

First, select Opportunity from Salesforce.

Let's go to SIL Manager, create a new file and write the following code:

SFDCConnection sfdcConnection = connectToSalesforce("My SFDC Connection");
SFDCOpportunity opp = sfdcGetOpportunity(sfdcConnection, "0064F00000NKA7CQAX");
runnerLog(opp);

As you can see, to write Opportunity data, we wrote only 2 lines of code. In the first line, we connect to Salesforce (I called my connection My SFDC Connection). In the second line, we get data from Salesforce using the Opportunity Id.

In order to change some field in Opportunity, we’ll write this code:

SFDCConnection sfdcConnection = connectToSalesforce("My SFDC Connection");
SFDCOpportunity opp;
opp.Description = "My new description";
sfdcUpdateOpportunity(sfdcConnection, "0064F00000NKA7CQAX", opp);

As you can see, the code is quite simple, and in addition, the documentation for the plugin contains examples for each function implemented in the plugin. Examples can be found  here .

But what if the Power Salesforce Connector does not suit you? In this case, we can create our own SIL functions.

Creating your own SIL functions


First, create the Salesforce connection function:

struct SFDCConnection {
    string access_token;
    string instance_url;
    string id;
    string token_type;
    string issued_at;
    string signature;
}

function connectToSalesforce(string consumer_key,
                      string consumer_secret,
                      string user_name,
                      string user_password) {
    HttpRequest request;
    HttpHeader header = httpCreateHeader("Content-Type", "application/json");
    request.headers += header;
    string dummyPayload;
    string url = "https://login.salesforce.com/services/oauth2/token";
    string url_string = "?grant_type=password&client_id=" + consumer_key+ 
                    "&client_secret=" + consumer_secret + 
                    "&username=" + user_name + 
                    "&password=" + user_password; 
    SFDCConnection connection = httpPost(url + url_string, request, dummyPayload);
    runnerLog(httpGetErrorMessage());
    return connection;
}

First, we created a JSON-based framework that returns from Salesforce when connected. And then they used the https://login.salesforce.com/services/oauth2/token method   to connect to Salesforce.

Now let's create a function to retrieve Opportunity data. To do this, we use the XX.X / sobjects / SObjectNam / id / method :

struct Opportunity {
    string Id;
    string AccountId;
    string Name;
    string Description;
    string StageName;
    string OwnerId;
}

function getOppFromOppId(string access_token,
                string url,
                string oppId) {
    Opportunity result;
    HttpRequest request;
    HttpHeader header = httpCreateHeader("Content-Type", "application/json");
    request.headers += header;
    header = httpCreateHeader("Authorization", "Bearer " + access_token);
    request.headers += header;
    result = httpGet(url + "/services/data/v39.0/sobjects/Opportunity/" + oppId, request);
    return result;
}

Again, we defined the structure for the Opportunity data, and then called the Salesforce Rest API method.

Now let's write a code that uses our functions:

SFDCConnection connection = connectToSalesforce(consumer_key, consumer_secret, user_name, user_password);
Opportunity opp = getOppFromOppId(connection.access_token, connection.instance_url, "0064F00000NKA7CQAX");
runnerLog(opp);

We got the same 2 lines of code. However, our full code looks like this:

struct SFDCConnection {
    string access_token;
    string instance_url;
    string id;
    string token_type;
    string issued_at;
    string signature;
}

function connectToSalesforce(string consumer_key,
                      string consumer_secret,
                      string user_name,
                      string user_password) {
    HttpRequest request;
    HttpHeader header = httpCreateHeader("Content-Type", "application/json");
    request.headers += header;
    string dummyPayload;
    string url = "https://login.salesforce.com/services/oauth2/token";
    string url_string = "?grant_type=password&client_id=" + consumer_key+ 
                    "&client_secret=" + consumer_secret + 
                    "&username=" + user_name + 
                    "&password=" + user_password; 
    SFDCConnection connection = httpPost(url + url_string, request, dummyPayload);
    runnerLog(httpGetErrorMessage());
    return connection;
}

struct Opportunity {
    string Id;
    string Name;
    string Description;
    string StageName;
    string OwnerId;
}

function getOppFromOppId(string access_token,
                string url,
                string oppId) {
    Opportunity result;
    HttpRequest request;
    HttpHeader header = httpCreateHeader("Content-Type", "application/json");
    request.headers += header;
    header = httpCreateHeader("Authorization", "Bearer " + access_token);
    request.headers += header;
    result = httpGet(url + "/services/data/v39.0/sobjects/Opportunity/" + oppId, request);
    return result;
}

string consumer_key = "your consumer key";
string consumer_secret = "your consumer secret";  
string user_name = "your user name";
string user_password = "password and user secret token";

SFDCConnection connection = connectToSalesforce(consumer_key, consumer_secret, user_name, user_password);
Opportunity opp = getOppFromOppId(connection.access_token, connection.instance_url, "0064F00000NKA7CQAX");
runnerLog(opp);

If you want, for example, updating Opportunity, then you need to implement your SIL function for this, using the Salesforce Rest API.

In addition, as can be seen in our example, we received the following fields from Opportunity: id, name, description, stagename and owner.

Suppose you want to add an accountid field. To do this, you need to change the structure as follows:

struct Opportunity {
    string Id;
    string Name;
    string Description;
    string StageName;
    string OwnerId;
    string AccountId;
}

We have added the AccountId field at the end of the structure.

It can be seen from the examples that if you write your SIL functions, you can implement a call to any Salesforce Rest API method, but for this you need to spend much more time writing your code. But then again, it's free.

All Articles