Ebase and Salesforce

Post any questions you have about using the Verj.io Studio, including client and server-side programming with Javascript or FPL, and integration with databases, web services etc.

Moderators: Jon, Steve, Ian, Dave

a129
Ebase User
Posts: 1
Joined: Fri Jun 19, 2015 2:07 pm

Ebase and Salesforce

#1

Postby a129 » Fri Jun 19, 2015 2:33 pm

Hi,

Does anyone have any expeirence interfacing Salesforce and EbaseXI?

Essentially I will want to create a Salesforce record when an Ebase form is submitted. I have an idea of what is required but it seems a little daunting to me just now. There is a range of API's available but to know where to start :(

Thanks and hi!
0 x

Dave
Ebase Staff
Posts: 89
Joined: Mon Sep 10, 2007 11:48 am

#2

Postby Dave » Fri Jun 19, 2015 4:11 pm

Hi - this is something of interest to us. We haven't actually done it but the principles are pretty clear and I think it should be straightforward. If you can contact us directly we'll more than happy to help you work it out.

Regards,
Dave
0 x

Segi
Ebase User
Posts: 649
Joined: Mon Dec 09, 2013 6:37 pm

#3

Postby Segi » Wed Jun 24, 2015 11:02 pm

Dave,

I would be interested in accessing SalesForce as well but I would want to do the opposite of what a129 wants to do. I'd like to run queries against our SF database instance and extract specific info. I know how to do this within the SF developer console but not how to access this data from eBase.

Thanks,

Segi
0 x

alexmcclune
Ebase User
Posts: 95
Joined: Wed Feb 27, 2013 5:16 pm

#4

Postby alexmcclune » Thu Jun 25, 2015 10:57 am

Hi,

This applies to us as well - in fact I would be interested in learning how to do both A129/Segi's suggestions (putting data into salesforce and extracting data from using Ebase).

We will be starting to use salesforce this year and some of our online forms will need to be integrated somehow quite quickly. Especially the ones that receive the most hits on our webpage.

If there are other users between ebase/salesforce already I'd like to hear from them, how they set things up, how difficult things are etc. Thanks.
0 x

Dave
Ebase Staff
Posts: 89
Joined: Mon Sep 10, 2007 11:48 am

#5

Postby Dave » Thu Jun 25, 2015 1:57 pm

OK, I did some R&D. Got it working via the REST API (didn't bother with the SOAP one) to the point of being successfully authorised, making a call and getting a response.

But the response tells me that API access for our trial account has yet to be granted! So, have asked them to that.

Soon as they do I will publish a sample that, say, gets a list of LEADS from SF, or adds a new LEAD. Should be enough to demonstrate the basics I think.
0 x

alexmcclune
Ebase User
Posts: 95
Joined: Wed Feb 27, 2013 5:16 pm

#6

Postby alexmcclune » Fri Jun 26, 2015 1:49 pm

Likewise I did a little R&D and tested the SOAP API this afternoon...pretty straight forward, whilst I haven't developed any Use Cases to actually use this yet I can see how it is built/fit together and should be useful.

Strangest thing was remembering that when I registered they forwarded me a Security Token, when logging in via the API you must remember to append the Security Token to your password.

Looking forward to the REST sample Dave.

BTW it is also worth noting that once logged in via the API the server endpoint (url) is amended for the user. When calling the login() make sure you amend the endpoint for any other web services to use response parameter sessionurl.

EDIT: I moved on and tried to create an object using SOAP. I am struggling with the create call from the SOAP API. I may need some Javascript assistance but I'll wait to see the REST demo.
0 x

Dave
Ebase Staff
Posts: 89
Joined: Mon Sep 10, 2007 11:48 am

#7

Postby Dave » Sun Jun 28, 2015 7:26 pm

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;
}
Last edited by Dave on Thu Jul 02, 2015 9:01 am, edited 6 times in total.
0 x

Dave
Ebase Staff
Posts: 89
Joined: Mon Sep 10, 2007 11:48 am

#8

Postby Dave » Mon Jun 29, 2015 2:07 pm

Sorry guys, you need an additional JAR in WEB-INF/lib:

http://central.maven.org/maven2/org/apa ... re-4.4.jar

Have amended the earlier post.

Dave
Last edited by Dave on Thu Jul 02, 2015 9:15 am, edited 1 time in total.
0 x

Dave
Ebase Staff
Posts: 89
Joined: Mon Sep 10, 2007 11:48 am

#9

Postby Dave » Mon Jun 29, 2015 4:37 pm

Added create, update, delete for good measure. Pretty much all the same stuff really though. Updated the earlier post with the new sample code.

NB. The new version needs another JAR in WEB-INF/lib:

https://org-json-java.googlecode.com/fi ... 120521.jar

Dave
Last edited by Dave on Thu Jul 02, 2015 9:16 am, edited 2 times in total.
0 x

alexmcclune
Ebase User
Posts: 95
Joined: Wed Feb 27, 2013 5:16 pm

#10

Postby alexmcclune » Tue Jun 30, 2015 9:10 am

Thanks Dave, it does indeed look logical. Essentially we will want to amend our current suite of forms to also create records in Salesforce - I am greatful you included the "create" to check that out.

Edit - > I kept receiving BAD GATEWAY errors but it looks like our firewall is causing me trouble again. On testing the form via my personal computers ebase install it worked fine.

I do notice I received an error "JSONObject" is not defined even when the json-20141113.jar file is place and I try to add/update.
0 x

Dave
Ebase Staff
Posts: 89
Joined: Mon Sep 10, 2007 11:48 am

#11

Postby Dave » Tue Jun 30, 2015 1:00 pm

Hi Alex - have contacted you directly.

Dave
0 x

Dave
Ebase Staff
Posts: 89
Joined: Mon Sep 10, 2007 11:48 am

#12

Postby Dave » Tue Jun 30, 2015 3:16 pm

ok, code and instructions posted above are clean now.

Dave
0 x

alexmcclune
Ebase User
Posts: 95
Joined: Wed Feb 27, 2013 5:16 pm

#13

Postby alexmcclune » Fri Apr 29, 2016 3:48 pm

It's been years but I am finally planning on developing this now.

We also just installed V5.1 and I see that REST Web Services have been added as a resource. I haven't got it working yet but this could be really nice and save us a lot of time.

http://forum.ebasetech.com/forum/viewto ... t=rest+web
0 x

Dave
Ebase Staff
Posts: 89
Joined: Mon Sep 10, 2007 11:48 am

#14

Postby Dave » Fri Apr 29, 2016 5:25 pm

cool, well as you say, use the REST stuff...will be a lot less code that we posted above! any help, you know where we are.
0 x

alexmcclune
Ebase User
Posts: 95
Joined: Wed Feb 27, 2013 5:16 pm

#15

Postby alexmcclune » Sat Apr 30, 2016 10:08 am

I think I need some of that help :P - fantastic addition the REST Web Service btw - so much simpler.

I've managed to create a resource to log in and request the new endpoint/access token. I've also managed to create a resource to query my salesforce Org but unfortunately I've got a little stuck on trying to insert a record into Salesforce.

Essentially I pass the values between the REST Resource and my form using form fields and the traditional mapping. I've created a Char field for the Body but obviously the body has to be of type JSON to submit to Salesforce.

I am getting a little (very) stuck on how to correctly create the JSON, convert it to string and call the web service. My attempts are below:

Import the necessary packages:

Code: Select all

importPackage (Packages.org.json); 
 importPackage (Packages.org.apache.http.entity);
Create the JSON:

Code: Select all

var newCase = new JSONObject();
	 newCase.put("contact", fields.FRM_CONTACTID.value);
   newCase.put("Status", 'New');
   newCase.put("Prority", 'Low');
   newCase.put("Subject", "New Case - ebase");
Here is where I am getting stuck, how do I convert the "newCase" to string so I can use the form field to pass the content to the resource?

I've tried:

Code: Select all

	var body = new StringEntity(newCase.toString(1));
	fields.SALESFORCE_NEW_CASE_BODY.value = setEntity(body);
and the next one reports: Java class "[Ljava.lang.reflect.Constructor;" has no public instance field or method named "toJSON".

Code: Select all

JSON.stringify(newCase);
Neither worked, both reported incompatable (I also tried using the Object field type but that was a hail mary) obviously I am missing something...
0 x

Jon
Moderator
Moderator
Posts: 1342
Joined: Wed Sep 12, 2007 12:49 pm

#16

Postby Jon » Tue May 03, 2016 8:19 am

You don't need to use Java to do this as Javascript provides everything that is necessary. It's very easy to convert between a Javascript object and a JSON string. So in your case, you can build the JSON string with something like:

Code: Select all

// create JS object containing parameters
var newCase = {};
newCase.contact = fields.FRM_CONTACTID.value;
newCase.Status = 'New';
newCase.Prority = 'Low';
newCase.Subject = "New Case - ebase"; 
// convert it to a JSON string
var s = JSON.stringify(newCase);
0 x

alexmcclune
Ebase User
Posts: 95
Joined: Wed Feb 27, 2013 5:16 pm

#17

Postby alexmcclune » Tue May 03, 2016 9:39 am

Perfect - thanks.
0 x


Who is online

Users browsing this forum: No registered users and 13 guests