Integration Workbook
Integration Workbook
@salesforcedocs
Last updated: June 30, 2015
© Copyright 2000–2015 salesforce.com, inc. All rights reserved. Salesforce is a registered trademark of salesforce.com, inc.,
as are other names and marks. Other marks appearing herein may be trademarks of their respective owners.
CONTENTS
One of the most frequent tasks Force.com developers undertake is integrating Force.com apps with existing applications. The tutorials
within this workbook are designed to introduce the technologies and concepts required to achieve this functionality.
The Force.com Integration Workbook is intended to be the companion to the Force.com Workbook. The series of tutorials provided here
extend the Warehouse application by connecting it with a cloud-based fulfillment app.
Intended Audience
This workbook is intended for developers who are new to the Force.com platform but have basic working knowledge in Java.
Tell Me More....
This workbook is designed so that you can go through the steps as quickly as possible. At the end of some steps, there is an optional
Tell Me More section with supporting information.
• You can find the latest version of this and other workbooks at
https://developer.salesforce.com/page/Force.com_workbook.
• To learn more about Force.com and to access a rich set of resources, visit Salesforce Developers at
https://developer.salesforce.com.
1
BEFORE YOU BEGIN
Before you begin the tutorials, you’ll need to install the Warehouse data model in your organization, create a Heroku developer account,
and install the Heroku Toolbelt software on your local workstation.
Note: After you’ve gone through this workbook, you can uninstall the Warehouse data model and sample data from your
organization by navigating to Installed Packages under Setup and deleting the Warehouse package.
2
TUTORIAL #1: CREATE A NEW HEROKU APPLICATION
Heroku provides a powerful Platform as a Service for deploying applications in a multitude of languages, including Java. In this tutorial,
you create a Web application using the Java Spring MVC framework to mimic handling fulfillment requests from our Warehouse
application.
Familiarity with Java is helpful, but not required for this exercise. The tutorial starts with an application template to get you up and
running. You then walk through the steps to securely integrate the application with the Force.com platform.
2. Execute the following command to log in to Heroku (followed by Heroku login credentials, if necessary):
heroku login
Heroku uses Git with SSH to deploy code. If you haven’t used SSH on this machine, you’ll need to create a public key after you provide
your Heroku login credentials. On Microsoft Windows, you might need to add your Git directory to your system path before you can
create a public key.
3
Tutorial #1: Create a New Heroku Application Step 3: Test the Application
Heroku creates a local Git repository as well as a new repository on its hosting framework, where you can push applications, and
adds the definition for that remote deployment for your local Git repository to understand. This makes it easy to leverage Git for
source control, make local edits, and deploy your application to the Heroku cloud.
All application names on Heroku must be unique, so you’ll see messages like the following when Heroku creates a new app:
Creating quiet-planet-3215... done
Important: The output above shows that the new application name is quiet-planet-3215. You might want to copy
and paste the generated name into a text file or otherwise make a note of it. Throughout this workbook, there are references
to the application name that look like {appname} that should be replaced with your application name. So, if your application
name is quiet-planet-3215, when a tutorial step prompts you to enter a URL with the format
https://{appname}.herokuapp.com/_auth, use:
https://quiet-planet-3215.herokuapp.com/_auth.
If prompted, select Yes to verify the authenticity of heroku.com. The deployment process will take a while as it copies files, grabs
any required dependencies, compiles, and then deploys your application.
5. Once the process is complete, you can preview the existing application by executing:
heroku open
Tell Me More...
Scroll back through the terminal log to the git push command, and you’ll see some magic. Early on, Heroku detects that the push
is a Spring MVC app, so it installs Maven, builds the app, and then gets it running for you, all with just a single command.
4
Tutorial #1: Create a New Heroku Application Summary
3. In another browser tab or window, open the Warehouse application on your Force.com instance.
4. Click Invoices and then select an existing invoice or create a new one if necessary.
5. In the browser URL bar, select the invoice record ID, which is everything after salesforce.com in the URL. It should look
something like a01E0000000diKc. Copy the ID to your clipboard.
6. Return to the browser window or tab showing your Heroku application.
7. Paste the invoice record ID into the field under Id.
8. Click Create. An order is created with the Invoice ID. Note that this order is distinct from a Salesforce order record.
9. Click OK. Your page looks something like:
Summary
Heroku’s polyglot design lets you easily deploy your applications with industry-standard tools, such as Git. Typically, teams use local
development environments, like Eclipse, and in fact Heroku has released an Eclipse plug-in for seamless integration with Eclipse. You
can also interact with Heroku on the command line and directly access logs and performance tools for your applications.
5
TUTORIAL #2: CONNECT THE WAREHOUSE APP WITH AN
EXTERNAL SERVICE
Force.com offers several ways to integrate with external systems. For example, without writing any code, you can declare workflow rules
that send outbound messages. You can implement more complex scenarios programmatically with Apex code.
This tutorial teaches you how to create a Web service callout to integrate the Warehouse app with the fulfillment application you deployed
in Tutorial 1. This fulfillment system, written in Java, is hosted on Heroku, but it could be any application with a Web service interface.
The following diagram illustrates the example scenario requirements: when an invoice’s status changes to Closed in your Force.com
system, the system sends a JSON-formatted message to the order fulfillment service running on Heroku, which then returns an order
ID to the Force.com system. The order ID is then added to the invoice.
6
Tutorial #2: Connect the Warehouse App with an External Step 3: Create an Integration Apex Class
Service
Tell Me More...
Just for fun, you can delete this remote site record and create and test the callout in Step 3 and Step 4 below to observe the error message
that is generated when an app attempts to call a URL without permission. Don’t forget to come back and add the remote site record
again, though!
// 1) see above
7
Tutorial #2: Connect the Warehouse App with an External Step 3: Create an Integration Apex Class
Service
// 2) see above
// 3) see above
8
Tutorial #2: Connect the Warehouse App with an External Step 4: Test the @future Method
Service
This small snippet of Apex code retrieves the ID for a single invoice and calls your @future method using this ID.
3. Select the Open Log checkbox.
4. Click Execute. You should see two entries in the logs at the bottom of the page. Double click the second line — it should have
Future Handler as its operation and a status of Success.
9
Tutorial #2: Connect the Warehouse App with an External Step 5: Create a Trigger to Call the @future Method
Service
5. Select the Filter checkbox under the Execution Log, above the Logs list, and then type DEBUG as the filter text. Scroll down and
double click the last line of the execution log. You should see a popup window with the response from the fulfillment Web service
that looks something like:
08:08:42:962 USER_DEBUG [58]|DEBUG|Fulfillment service returned
[{"order_number":2,"id":"a01E0000009RpppIAC"}]
Now that you have a functional @future method that can call the fulfillment Web service, it's time to tie things together with a trigger.
3. Click Save.
The comments in the code explain what the code does. In particular, understand that Force.com triggers must be able to handle both
single-row and bulk updates because of the varying types of calls that can fire them (single-row or bulk update calls). The trigger creates
a list of invoice IDs that have been closed in this update, and then calls the @future method once, passing the list of IDs.
10
Tutorial #2: Connect the Warehouse App with an External Step 6: Test the Complete Integration Path
Service
The following screen shows the Invoices tab after the asynchronous call has returned the new order ID:
11
Tutorial #2: Connect the Warehouse App with an External Summary
Service
Summary
Congratulations! Your app is sending invoices for fulfillment. You have successfully created an asynchronous Apex class that posted
invoice details to your fulfillment app hosted on Heroku. Of course, your external application could reside anywhere as long as you have
access via Web services. Your class uses open standards including JSON and REST to transmit data, and a trigger on invoices to execute
the process.
12
TUTORIAL #3: UPDATE THE HEROKU APP
You now have two sides of an integration in place: one running a Java endpoint on Heroku, and another in Force.com which communicates
with the endpoint when the appropriate changes take place. Now that you’ve got the connection in place, update the Heroku application
to retrieve the pertinent information and display it to the user.
Note: Be sure to replace {appname} with your actual Heroku app name.
8. In the Selected OAuth Scopes field, select Full access (full) and Perform requests on your
behalf at any time (refresh_token, offline_access), and then add them to the selected OAuth scopes.
9. Click Save.
13
Tutorial #3: Update the Heroku App Step 3: View the Invoice Information
3. You need to set your Access keys to your Heroku application. Enter:
Replace PUBLICKEY with the Consumer Key. Similarly, replace PRIVATEKEY with the Consumer Secret. It may be
helpful to do this in a text editor before putting it on the command line.
5. In a browser tab or window, navigate to https://{appname}.herokuapp.com to see the changes (refresh your browser
if needed).
By adding an OAuth flow to your app, your app can request a user’s permission to work with session information without requiring the
third-party server to handle the user’s credentials. With this functionality added to the project, the fulfillment application can use the
Force.com REST API to access information directly from the user’s instance.
Tell Me More...
You can review all of the changes brought in by this branch on GitHub at:
https://github.com/sbob-sfdc/spring-mvc-fulfillment-base/compare/master...full. Notice that
the changes use the Force.com REST API to manipulate invoice records. Look at InvoiceServiceImpl.java in particular to see
how it creates, queries, retrieves and deletes invoices. This tutorial uses the findOrder() method only. The other methods are
included for your reference.
Tell Me More...
14
Tutorial #3: Update the Heroku App Summary
Notice that the Web service call from Force.com to create orders is not secured. Securing the call goes beyond the scope of this workbook,
but a simple solution would be to set up a shared secret between the Force.com app and the fulfillment app. The Force.com app would
create an HMAC signature from the parameters in the request, using the secret, and the fulfillment app would verify the signature.
Summary
Congratulations! Your fulfillment app now retrieves invoice information via the Force.com REST API and displays it to the user. You
configured your app in Salesforce to use OAuth for authentication, and you added OAuth credentials to your app hosted on Heroku.
You can further modify your app to manipulate invoice information however you want.
15
TUTORIAL #4: ADD YOUR APP TO SALESFORCE USING
FORCE.COM CANVAS
You’ve done a lot already. Let’s go one step further and make your app accessible for your users right from within Salesforce. Force.com
Canvas enables you to easily integrate a third-party application in Salesforce. Force.com Canvas is a set of tools and JavaScript APIs that
you can use to expose an application as a canvas app. This means you can take your new or existing applications and make them available
to your users as part of their Salesforce experience.
4. In a browser tab or window, navigate to https://{appname}.herokuapp.com to see the changes (refresh your browser
if needed).
Tell Me More...
You can review all of the changes brought in by this branch on GitHub at
https://github.com/sbob-sfdc/spring-mvc-fulfillment-base/compare/full...canvas.
Notice that the new branch uses the signed request from the Force.com Canvas API and not the Heroku-initiated OAuth from Tutorial
3, Step 2. The new branch also uses the Force.com REST API to manipulate invoice records. Look at CanvasUiController.java
in particular to see how it retrieves, parses, and sets the signed request for use by the app. Also, order.jsp has changed to present
an easier-to-use screen on the invoice page layout. This tutorial has only set the signed request for use on the canvasui page and
the orders page in the app.
Step 2: Edit the Connected App Details and Enable the App for
Force.com Canvas
We’ve already configured your connected app. Now we need to enable and configure it for Force.com Canvas.
1. From Setup, click Create > Apps.
2. In the Connected App Settings section, select your application and click Edit.
3. In the Canvas App Settings section, select the Force.com Canvas checkbox.
4. In the Canvas App URL field, enter https://{appname}.herokuapp.com/canvasui.
5. In the Access Method field, select Signed Request (Post).
6. In the Locations field, select Chatter Tab and Visualforce Page, and then add them to the selected locations.
16
Tutorial #4: Add Your App to Salesforce Using Force.com Step 2: Edit the Connected App Details and Enable the App
Canvas for Force.com Canvas
7. Click Save.
If you look at CanvasUiController.java, you’ll see something like the following, which shows Heroku obtaining a signed
request and validating it. We’re leveraging the OAUTH_CLIENT_SECRET Heroku key set in Tutorial 3, Step 2 to validate the signed
request.
@Controller
@RequestMapping(value="/canvasui")
public class CanvasUIController {
@Autowired
private OrderService orderService;
@Autowired
private InvoiceService invoiceService;
@Autowired
public CanvasUIController(Validator validator) {
this.validator = validator;
}
@RequestMapping(method= RequestMethod.POST)
public String postSignedRequest(Model model,
@RequestParam(value="signed_request")String signedRequest, HttpServletRequest request){
Invoice invoice;
try {
invoice = invoiceService.findInvoice(order.getId());
} catch (ApiException ae) {
// No match
invoice = new Invoice();
17
Tutorial #4: Add Your App to Salesforce Using Force.com Step 2: Edit the Connected App Details and Enable the App
Canvas for Force.com Canvas
}
model.addAttribute("invoice", invoice);
return "order";
}
}
return getOrdersPage(model);
}
@RequestMapping(method=RequestMethod.GET)
public String getOrdersPage(Model model) {
model.addAttribute("order", new Order());
model.addAttribute("orders", orderService.listOrders());
return "orders";
}
After the validation, the signed request is passed to order.jsp, where the browser can access it.
<%@ page session="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
<title>Order</title>
<link rel="stylesheet" href="<c:url value="/resources/blueprint/screen.css" />"
type="text/css" media="screen, projection">
<link rel="stylesheet" href="<c:url value="/resources/blueprint/print.css" />"
type="text/css" media="print">
<!--[if lt IE 8]>
<link rel="stylesheet" href="<c:url value="/resources/blueprint/ie.css" />"
type="text/css" media="screen, projection">
<![endif]-->
<script type="text/javascript" src="<c:url value="/resources/jquery-1.4.min.js" /> ">
</script>
<script type="text/javascript" src="<c:url value="/resources/json.min.js" /> ">
</script>
<script type="text/javascript" src="<c:url value="/resources/canvas-all.js" /> ">
</script>
<script>
// Get the Signed Request from the CanvasUIController
var sr = JSON.parse('${not empty signedRequestJson?signedRequestJson:"{}"}');
18
Tutorial #4: Add Your App to Salesforce Using Force.com Step 2: Edit the Connected App Details and Enable the App
Canvas for Force.com Canvas
// This function will be called when the "Delete Order" button is clicked.
// It will delete the record from the Heroku database.
function deleteHandler(){
$.deleteJSON("/order/${order.orderId}", function(data) {
alert("Deleted order ${order.orderId}");
location.href = "/orderui";
}, function(data) {
alert("Error deleting order ${order.orderId}");
});
return false;
}
// This function gets the instance the user is on for a page referesh
function getRoot() {
return sr.client.instanceUrl;
}
</script>
</head>
<body>
<div id="bodyDiv" style="width:inherit;">
<div id="myPageBlockTable">
<h2 id="OrderTitle">
Order Number: <c:out value="${order.orderId}"/>
19
Tutorial #4: Add Your App to Salesforce Using Force.com Step 3: Configure Access to Your Force.com Canvas App
Canvas
</h2>
<table id="myTable" width="100%">
<col width="20%">
<tr><td class="myCol">Invoice Id:</td><td class="valueCol">
<c:out value="${invoice.id}"/></td></tr>
<tr><td class="myCol">Invoice Number:</td><td class="valueCol">
<c:out value="${invoice.number}"/></td></tr>
<tr><td class="myCol">Status:</td><td class="valueCol" valign="center">
<c:out value="${invoice.status}"/>
<!-- Display a green check if the order is Shipped, or a red x if
not shipped -->
<c:choose>
<c:when test="${invoice.status == 'Shipped'}">
<img src="/resources/images/shipped.png" />
</c:when>
<c:otherwise>
<img src="/resources/images/pending.png" />
</c:otherwise>
</c:choose>
</td></tr>
</table>
<!-- Display the Back and Delete Order Button if viewed outside
of salesforce (no signed request). -->
<!-- Display the Finalize Button if viewed inside of salesforce and
the Status is not Shipped. -->
<c:choose>
<c:when test="${empty signedRequestJson}">
<button onclick="location.href='/orderui'">Back</button>
<button id="deleteButton">Delete Order</button>
</c:when>
<c:otherwise>
<c:if test="${invoice.status ne 'Shipped'}">
<button id="finalizeButton">Finalize</button>
</c:if>
</c:otherwise>
</c:choose>
</div>
</div>
</body>
</html>
20
Tutorial #4: Add Your App to Salesforce Using Force.com Step 4: Make Your Force.com Canvas App Available from
Canvas the Chatter Tab
Now you’ll use profiles and permission sets to define who can see your canvas app. In this example, we’ll allow anyone with the
System Administrator profile to access the app.
5. In the Connected App Detail page’s Profiles related list, click Manage Profiles.
6. Select the System Administrator profile, and then click Save.
Your app is now available to anyone with the System Administrator profile.
Step 4: Make Your Force.com Canvas App Available from the Chatter
Tab
The values you selected in the Locations field when creating the connected app in Step 2: Edit the Connected App Details and
Enable the App for Force.com Canvas on page 16 determine where an installed canvas app appears. When an app is made available
to the Chatter tab, there’s nothing we need to do for this step. If you log into your Salesforce org and select the Chatter tab, you’ll see
that your canvas app appears in the app navigation list.
Note: When displaying the list of orders on the Chatter Tab, remember that orders.jsp has been set up to handle the signed
request POST. However, if you click into a record from this page, you are redirected to orderui, which uses OAuth. If the Heroku
OAuth flow is inactive, you may receive an error when viewing the individual order.
Click your app’s name. It should look something like this:
21
Tutorial #4: Add Your App to Salesforce Using Force.com Step 5: Use Visualforce to Display the Canvas App on a
Canvas Record
3. In Label, enter FulfillmentCanvas. You use this label to identify the page in Setup tools when performing actions such
as defining custom tabs or overriding standard buttons.
4. In Name, accept the default name FulfillmentCanvas.
5. Add the following markup to the Visualforce Markup box, making sure to replace {appname} with your Heroku application name,
and then click Save.
<apex:page standardController="Invoice__c">
<apex:canvasApp developerName="{appname}"
parameters="{'orderId':'{!Invoice__c.OrderId__c}'}" width="100%"/>
</apex:page>
22
Tutorial #4: Add Your App to Salesforce Using Force.com Summary
Canvas
Notice the Finalize button in the canvas app. If the invoice isn’t in 'Shipped' status, the red “X” and Finalize will show in the app. If you
click Finalize, Heroku uses the Force.com Canvas API to call the REST API and update the invoice Status field. Once the status is set to
'Shipped', the red “X” is replaced and Finalize is hidden.
Summary
Congratulations! With a combination of OAuth authentication, Force.com REST API, Apex triggers, @future callouts, the polyglot
framework of the Heroku platform, Force.com Canvas, and Visualforce, you created and deployed a bi-directional integration between
two clouds.
This workbook covers just one example of the many ways to integrate your applications with Salesforce. One integration technology
that we didn’t mention is the Streaming API that lets your application receive notifications from Force.com whenever a user changes
Salesforce data. You can use this in the fulfillment application to monitor when changes are made to invoices and to automatically
update the application pages accordingly. Visit https://developer.salesforce.com to learn more about all the ways you
can integrate your application with Salesforce.
23