Posted 08/18/2010 - 18:21 by kishores
I want to build a Javascript App using Presto to allow a user to interact with his data inside Salesforce.com. Let me describe what I would like to do.
First, my Salesforce App needs access to data in Salesforce. I would like to use a REST API which will allow me to get the results back in JSON. So ideally, I want to query Salesforce using a REST-like API by passing in the SOQL as a query string parameter and get back results in JSON. Something similar to:
http://server:port/SalesforceQuery?soql=”select * from Account”&output=json
Second, my Salesforce App must be cross-domain because it can be deployed anywhere with just a script embed tag, without any proxies. So, ideally, I would like to use JSONP in my App to get past the cross-domain issues associated with XHR.
Now, here is the kicker. To my surprise, I was not able to find any REST API! Salesforce does not support a REST API (see here and here) despite a huge interest from developers like me.
So, I decided to roll up my sleeves and create a easy to use Salesforce Query API using Presto. Let me now walk you through the steps.
Implementing the Salesforce Query Mashup using EMML
Log in to the Presto Cloud at: http://prestocloud.jackbe.com/prestohub/home.html. You must be a member of JackBe’s Mashup Developer Community to be access Presto Cloud. If you are not a member, register now, it is free and simple.
A basic knowledge of EMML is required to build this mashup using the Mashup Editor. You can read more about EMML (Enterprise Mashup Markup Language) here. It is a XML based language for creating enterprise mashups.
Step 1: Register the salesforce WSDL as a “Mashable” in Presto


Step 2: Open Mashup Editor to create a Salesforce Mashup
Click Create > Mashup in Editor in the Presto Hub main menu to open the Mashup Editor in a new window. The Editor also creates a new EMML template for you.
Lets step back and think about the API for this Mashup. We would like the SOQL (Salesforce Query) to be provided as an input to this mashup. We expect the output of this mashup to be the response from Salesforce API. To do this, let's add an input tag in the mashup EMML.
All Salesforce APIs such as query require a sessionId and a serverUrl. The serverUrl is the Service endpoint provided by Salesforce upon successful login. Let's add the login call to our mashup. To do this, search for the Salesforce mashable in Mashup Editor and expand it to see the available operations. Click the login operation and the Mashup Editor fills in the EMML required to invoke the login operation.
To run the EMML that we have built so far, enter the username and the password for the demo account with salesforce.com. Then click Run to see the output of the mashup. Note that the output of the <invoke> statement is now pointing to the result output variable which is the name of the <output> tag. The Run area shows the value of the <output> tag which is a XML document that is returned from the Saleforce login API call.
Now that we have the response of the login operation, we can proceed to invoke the Salesforce query operation. Click the query operation in the menu and Mashup Editor automatically adds the required EMML.

The Salesforce Web Service requires us to use the Service Endpoint that is returned as part of the login response. This is slightly different from most other Web Services where the endpoint is typically derived from the URL of the WSDL. To account for this requirement, we must pass the <serviceEndpoint> as part of the <header> so Presto can use the dynamically provided serverURL instead of the static URL. This header is constructed in EMML in the <constructor> tag (see above).
The SessionId and other Input Parameters
The Salesforce query operation also requires us to pass the sessionId as part of the Service Header and to pass the Query string in the body of the request. We already have these values in variables in our EMML. Let's assign the value of these variables to the header and body.
Now that we have all the inputs set up and the invocations wired correctly, let's click Run to see if we get the response from Salesforce WebService.

Save the mashup by clicking Save or Save As and provide a meaningful name, description and other properties. I saved it as Salesforce SOQL Mashup. Let's open the mashup in Presto Hub. You an also search for the mashup using its name from the Search field in the main menu bar (top right corner).

The default value fo the SOQL is automatically provided in the EMML. Click Run to see the results of this mashup invocation.

Technical Specs & the REST API
Great ! We are now able to run the mashup by just providing the SOQL as an input. Now, lets see how the mashup can be invoked. To get the REST API URL, click the Technical Specs tab in the mashup's artifact page.


The Technical Specs tab in mashup and mashable artifact pages also provides samples on how to run the mashup or mashable using the Presto Connect for JavaScript (PC4JS) API, which supports Cross Domain requests, with various JavaScript Frameworks such as jQuery or ExtJS. Let's see how this can be done. Copy the sample HTML code from the Presto Connect tab in Technical Specs into a local HTML file. I named it InvokeSOQL.html. (Download the InvokeSOQL.txt attachment and rename it to .html when you save it locally.) I made a few minor modifications to add the full Presto Cloud URL as a prefix to the sample: <script type="text/javascript" src="http://prestocloud.jackbe.com/prestohub/jsapi/loader.js"></script>
url: "http://prestocloud.jackbe.com/presto/edge/api/rest/Salesforce_SOQL_Mashup/Invoke?x-presto-resultFormat=json"
Now, open the HTML file from your local file system and click Run. You should see the JSON response from the Salesforce SOQL Mashup that we built.
We just invoked the Salesforce SOQL mashup using a simple REST API and Cross-Domain !!

In my next posts, I will discuss in detail about using the above API and variations of this API for creating custom Presto Apps.
- kishores's blog
- Login or register to post comments
- Email this page
