X
X

Login

Login if you already have an account

LOGIN

Register

Create a new account. join us!

REGISTER

Support

Need some help? Check out our forum.

FORUM
February 7, 2013 kevin nickels

FYI – it is easy to integrate your backend with other web services

As a Developer, I want to integrate with other web services so that I can offload the integration tasks from my client to my backend.

Many times, you may want to utilize some other web service for the purposes of backend integration. FatFractal makes this really easy.

Let’s say you want to interact with SalesForce data from your backend…

FatFractal support the Common.js standard which includes an http client that let’s you do just about anything you want.

Example:

Here is some code that interfaces to to securely retrieve some information from a SalesForce API:

var hc = require('ringo/httpclient');

exports.getQs = function() {
var loginUrl = "https://login.salesforce.com/services/oauth2/token";
var apiVersion = "v25.0";
var username = "someUsername";
var password = "somePassword";
var securityToken = "someSecurityToken";
var clientId = "someClientId";
var clientSecret = "someClientSecret";

function LoginSF(username, password, securityToken, clientId, clientSecret) {
var data = {
grant_type: "password",
client_id: clientId,
client_secret: clientSecret,
username: username,
password: password + securityToken
};
var response = JSON.parse(hc.post(loginUrl, data).content);
return response;
}

function QuerySF(instanceUrl, accessToken, queryString) {
var url = instanceUrl + "/services/data" + apiVersion + "/query";
var data = {q: queryString};

var request = {
url: url,
data: data,
method: "GET",
headers: {
Authorization: "Bearer " + accessToken
}
};

var response = JSON.parse(hc.request(request).content);
return response;
}
// perform query
var loginResponse = LoginSF(username, password, securityToken, clientId, clientSecret, true);
var instanceUrl = loginResponse.instance_url;
var accessToken = loginResponse.access_token;
var queryResponse = QuerySF(instanceUrl, accessToken, "SELECT A__c, B__c, C__c FROM D__c");

var records = queryResponse.records;
for (var i = 0; i < records.length; i++) {
// DO SOME STUFF
// E.G. RETURN SOME DATA (SERVER EXTENSION)
// OR TAKE SOME ACTION (EVENT HANDLER)
}
}

What this does is define a function that can be used with an Event Handler or as an API extension with a Server Extension that logs in to an API on SalesForce.com, retrieves an instance_url and access_token that are need to then to execute a query and retrieve a response.

Many other things can be done with this powerful client – have fun!

Kevin

Tagged: , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Contact