So, salesforce kindly authorised API access to our trial a/c.
The following code gets a list of LEADS from a saleforce.com account and adds them into an Ebase table. I think it demonstrates the basic mechanics but I'll be happy to add in create/update/delete of LEADS as well, if anyone would like to see it.
You'll need to add the following JARs into WEB-INF/lib to make it work:
http://central.maven.org/maven2/org/apa ... -4.3.3.jar
http://central.maven.org/maven2/org/apa ... re-4.4.jar
https://org-json-java.googlecode.com/fi ... 120521.jar
and naturally you'll need to add in your own username, password, client ID, client secret and security token to the code.
If anyone is struggling to get hold of these salesforce parameters please contact me directly and I'll share access to Ebase's trial account.
Anyway, here's the code:
Code: Select all
importPackage(com.ebasetech.xi.api);
importPackage(com.ebasetech.xi.services);
// HTTP packages
importPackage (Packages.org.apache.http.client.methods);
importPackage (Packages.org.apache.http.message);
importPackage (Packages.org.apache.http.util);
importPackage (Packages.org.apache.http.impl.client);
importPackage (Packages.org.apache.http.entity);
// JSON
importPackage (Packages.org.json);
// salesforce.com parameters
var username = "username@salesforce.com";
var password = "asswordSecurityToken";
var loginURL = "https://login.salesforce.com";
var grantService = "/services/oauth2/token?grant_type=password";
var clientID = "ConsumerKeyFromSalesforceConnectedApps";
var clientSecret = "ConsumerSecretFromSalesforceConnectedApps";
var restEndpoint = "/services/data" ;
var apiVersion = "/v32.0" ;
// The basic process is:
// 1. Authorise your access. If sucessful you'll be returned an access token and the REST api URL of the salesforce instance where your account is
// 2. Use the access token to execute a REST api call to the URL you received
//
// In this example we get back a JSON object containing a list of sales leads. We just take them out and put them into an Ebase table.
//
try {
// try to login to salesforce.com, returns a JSON object containing an access token and the REST instance URL
var loginDetails = salesForceLogin(loginURL, grantService, clientID, clientSecret, username, password);
// now use these to call salesforce.com REST API and return a JSON object containing a list of LEADS
var leads = salesForceQuery(apiVersion, restEndpoint, loginDetails.instance_url, loginDetails.access_token, "/query?q=Select+Id+,+FirstName+,+LastName+,+Company+From+Lead+Limit+100");
// collect the data from leads and write it to an Ebase table
getLeads(leads);
// add a new lead, return a JSON response and pick up the new lead ID from it
var newLeadID = addLead('Mickey', 'Mouse', 'Walt Disney Corporation', loginDetails.instance_url, loginDetails.access_token, "/sobjects/Lead/").id;
// update the lead we just created, i.e. change name from Mickey to Donald
updateLead('Donald', 'Duck', 'Walt Disney Corporation', loginDetails.instance_url, loginDetails.access_token, "/sobjects/Lead/", newLeadID);
// now delete our new LEAD, just for fun
deleteLead(loginDetails.instance_url, loginDetails.access_token, "/sobjects/Lead/", newLeadID);
}
catch(e) {
log(e);
}
// login to salesforce
function salesForceLogin(loginURL, grantService, clientID, clientSecret, username, password) {
// assemble the login request URL
var login = loginURL +
grantService +
"&client_id=" + clientID +
"&client_secret=" + clientSecret +
"&username=" + username +
"&password=" + password;
try {
// get an HTTP client and post the URL
var httpclient = HttpClientBuilder.create().build();
var httpPost = new HttpPost(login);
var response = httpclient.execute(httpPost);
if (response.getStatusLine().getStatusCode() == 200) {
// response status is OK so get the result of the url post and return it is a JSON object
var result = EntityUtils.toString(response.getEntity());
return JSON.parse(result);
}
}
catch(e) {
log(e);
}
finally {
httpPost.releaseConnection();
}
}
// call salesforce REST api with a particular query to execute
function salesForceQuery(apiVersion, restEndpoint, instanceUrl, accessToken, query) {
try {
// get an HTTP client
var httpClient = HttpClientBuilder.create().build();
// get an HttpGet object and add the access token to the header
var httpGet = new HttpGet(instanceUrl + restEndpoint + apiVersion + query);
httpGet.addHeader(new BasicHeader("Authorization", "OAuth " + accessToken));
// get the response
var response = httpClient.execute(httpGet);
var result = EntityUtils.toString(response.getEntity());
// return the result as a JSON object
return JSON.parse(result);
}
catch (e) {
log(e);
}
finally {
httpGet.releaseConnection();
}
}
// add a new LEAD
function addLead(firstName, lastName, company, instanceUrl, accessToken, leadPath) {
// construct the new lead as a JSON object
var lead = new JSONObject();
lead.put("FirstName", firstName);
lead.put("LastName", lastName);
lead.put("Company", company);
// post the request
try {
// get an HTTP client
var httpClient = HttpClientBuilder.create().build();
// get an HttpGet object and add the access token to the header
var httpPost = new HttpPost(instanceUrl + restEndpoint + apiVersion + leadPath);
httpPost.addHeader(new BasicHeader("Authorization", "OAuth " + accessToken));
// set up the message to send
var body = new StringEntity(lead.toString(1));
body.setContentType("application/json");
httpPost.setEntity(body);
// get the response
var response = httpClient.execute(httpPost);
var result = EntityUtils.toString(response.getEntity());
// return the result as a JSON object
return JSON.parse(result);
}
catch(e) {
log(e);
}
finally {
httpPost.releaseConnection();
}
}
// update a LEAD
function updateLead(firstName, lastName, company, instanceUrl, accessToken, leadPath, leadID) {
// construct the lead to be updated as a JSON object
var lead = new JSONObject();
lead.put("FirstName", firstName);
lead.put("LastName", lastName);
lead.put("Company", company);
// post the request
try {
// get an HTTP client
var httpClient = HttpClientBuilder.create().build();
// get an HttpGet object and add the access token to the header
var httpPatch = new HttpPatch(instanceUrl + restEndpoint + apiVersion + leadPath + leadID);
httpPatch.addHeader(new BasicHeader("Authorization", "OAuth " + accessToken));
// set up the message to send
var body = new StringEntity(lead.toString(1));
body.setContentType("application/json");
httpPatch.setEntity(body);
// get the response
var response = httpClient.execute(httpPatch);
if (response.getStatusLine().getStatusCode() == 204) return true;
return false;
}
catch(e) {
log(e);
}
finally {
httpPatch.releaseConnection();
}
}
// delete a LEAD
function deleteLead(instanceUrl, accessToken, leadPath, leadID) {
// post the request
try {
// get an HTTP client
var httpClient = HttpClientBuilder.create().build();
// get an HttpGet object and add the access token to the header
var httpDelete = new HttpDelete(instanceUrl + restEndpoint + apiVersion + leadPath + leadID);
httpDelete.addHeader(new BasicHeader("Authorization", "OAuth " + accessToken));
// get the response
var response = httpClient.execute(httpDelete);
if (response.getStatusLine().getStatusCode() == 204) return true;
return false;
}
catch(e) {
log(e);
}
finally {
httpDelete.releaseConnection();
}
}
// add leads to an Ebase table
function getLeads(leads) {
// initialise the Ebase table
tables.LEADS.resetTable();
// get the data from the passed in JSON object
leads.records.forEach(function(h) {
insertLeadsToEbase(h.Id,h.FirstName,h.LastName,h.Company)
});
}
// insert rows to the LEADS Ebase table
function insertLeadsToEbase(ID,firstName,lastName,company) {
tables.LEADS.insertRow();
tables.LEADS.ID.value = ID;
tables.LEADS.FIRSTNAME.value = firstName;
tables.LEADS.LASTNAME.value = lastName;
tables.LEADS.COMPANY.value = company;
}