SIL and Salesforce

Hello!

In this article I would like to talk on the options to work with Salesforce from SIL.

SIL is a programming language to automate actions in Atlassian Jira and Confluence. You can find more information here.

There are two options to work from SIL with Salesforce:

  1. Use Power Salesforce Connector.
  2. Write your own SIL routines.

In this article we will discuss the two available options. But first you need to prepare Salesforce for using it later in SIL.

Prepare Salesforce

To start to perform actions in Salesforce we need to setup a connected app in Salesforce. The connected app will allow us to use Salesforce Rest API.

To create a connected app go to cog wheel -> Setup:

Setup.png

Choose App -> App Manager:

App Manager.png

Push the New Connected App button:

Required Fields.png

Click the checkbox to enable OAuth settings. The callback URL is not used in the integration. Select Full Access and click Add. Then click Save.

On the next page, under OAuth Settings, you'll see your Consumer Key and the option to reveal and get your Consumer Secret

Also you need to get your user secret token in Salesforce.

To get the security token of a user. Go to User Settings → Reset My Security Token. Reset the security token and you will receive a new security token to the user email address:

 

Screenshot 2020-04-23 at 18.58.15.png

Power Salesforce Connector

Power Salesforce connector provides SIL routines for working with Accounts, Opportunities and execute SOQL queries (you can find all available routines here). It is very convenient because you do not need to write your own SIL routines. The disadvantages of the Power Salesforce Connector are:

Suppose, that you want to use the Power Salesforce Connector. First install it to you Jira instance and open cog wheel -> manage apps -> SFDC Connection Configuration:

Screenshot 2020-04-23 at 19.40.28.png

Push the Add connection button:

Screenshot 2020-04-23 at 19.41.56.png

Fill the required fields and push the Save button.

Now we can write code in the SIL Manager. Let's try to select an opportunity from Salesforce.

The SIL code would look like this:

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

You see we have only two lines of code to get our opportunity. It is very convenient. In the first line we make connection to Salesforce (I called my connection as My SFDC Connection when I created configuration in the Power Salesforce Connection settings). In the second line we query the opportunity.

To update a field in the opportunity we would write a code like this:

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

It is pretty straightforward to use the SIL routines from the Power Salesforce Connector app. Also you can find examples in the documentation for this app.

But what if you do not want to use the Power Salesforce Connector app? Then you can create your own SIL routines

Create your own SIL routines

First let's create a routine for Salesforce connection:

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 struct with our connection fields. Then we used the https://login.salesforce.com/services/oauth2/token rest api to connect to Salesforce.

Now let's create a SIL routine to get an opportunity data:

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;
}

Now let's use our routines and get an opportunity data:

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

Now we've got the same two lines to get an opportunity data. But our complete script would look 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);

So if you want to use any other Salesforce Rest API you would need to create your own SIL routine, which would look like getOppFromOppId routine but had a different return type and Rest API method.

Also in our code we receive the following data from an opportunity: the id, the name, the description, the stage name and the owner.

Suppose, you want also to get the account id. In this case you would change the Opportunity structure like this:

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

So the option to create your own SIL routines is more flexible and free of charge, but it requires you to write more code.

1 comment

Gonchik Tsymzhitov
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 23, 2020

That's great! :) Thank you

Comment

Log in or Sign up to comment
TAGS
AUG Leaders

Atlassian Community Events