Salesforce component

Available as of Camel 2.12

This component supports producer and consumer endpoints to communicate with Salesforce using Java DTOs. There is a companion maven plugin Camel Salesforce Plugin that generates these DTOs (see further below).

Maven users will need to add the following dependency to their pom.xml for this component:

URI format

The URI scheme for a salesforce component is as follows

You can append query options to the URI in the following format, ?option=value&option=value&...

Supported Salesforce APIs

The component supports the following Salesforce APIs

Producer endpoints can use the following APIs. Most of the APIs process one record at a time, the Query API can retrieve multiple Records.

For example, the following producer endpoint uses the upsertSObject API, with the sObjectIdName parameter specifying 'Name' as the external id field. The request message body should be an SObject DTO generated using the maven plugin. The response message will either be null if an existing record was updated, or CreateSObjectResult with an id of the new record, or a list of errors while creating the new object.

Rest Bulk API

Producer endpoints can use the following APIs. All Job data formats, i.e. xml, csv, zip/xml, and zip/csv are supported. The request and response have to be marshalled/unmarshalled by the route. Usually the request will be some stream source like a CSV file, and the response may also be saved to a file to be correlated with the request.

createJob - Creates a Salesforce Bulk Job

getJob - Gets a Job using its Salesforce Id

closeJob - Closes a Job

abortJob - Aborts a Job

createBatch - Submits a Batch within a Bulk Job

getBatch - Gets a Batch using Id

getAllBatches - Gets all Batches for a Bulk Job Id

getRequest - Gets Request data (XML/CSV) for a Batch

getResults - Gets the results of the Batch when its complete

createBatchQuery - Creates a Batch from an SOQL query

getQueryResultIds - Gets a list of Result Ids for a Batch Query

getQueryResult - Gets results for a Result Id

For example, the following producer endpoint uses the createBatch API to create a Job Batch. The in message must contain a body that can be converted into an InputStream (usually UTF-8 CSV or XML content from a file, etc.) and header fields 'jobId' for the Job and 'contentType' for the Job content type, which can be XML, CSV, ZIP_XML or ZIP_CSV. The put message body will contain BatchInfo on success, or throw a SalesforceException on error.

Rest Streaming API

Consumer endpoints can use the following sytax for streaming endpoints to receive Salesforce notifications on create/update.

To create and subscribe to a topic

To subscribe to an existing topic

Examples

Uploading a document to a ContentWorkspace

Create the ContentVersion in Java, using a Processor instance:

Give the output from the processor to the Salesforce component:

Using Salesforce Limits API

With salesforce:limits operation you can fetch of API limits from Salesforce and then act upon that data received. The result of salesforce:limits operation is mapped to org.apache.camel.component.salesforce.api.dto.Limits class and can be used in a custom processors or expressions.

For instance, consider that you need to limit the API usage of Salesforce so that 10% of daily API requests is left for other routes. The body of output message contains an instance of org.apache.camel.component.salesforce.api.dto.Limits object that can be used in conjunction with Content Based Router and Spring Expression Language (SpEL) to choose when to perform queries. Notice how multiplying 1.0 with the integer value held in body.dailyApiRequests.remaining makes the expression evaluate as with floating point arithmetic, without it - it would end up making integral division which would result with either 0 (some API limits consumed) or 1 (no API limits consumed).

Working with approvals

All the properties are named exactly the same as in the Salesforce REST API prefixed with approval. You can set approval properties by setting approvalPropertyName of the Endpoint these will be used as template -- meaning that any property not present in either body or header will be taken from the Endpoint configuration. Or you can set the approval template on the Endpoint by assigning approval property to a reference onto a bean in the Registry

You can also provide header values using the same approvalPropertyName in the incoming message headers.

And finally body can contain one AprovalRequest or an java.util.Iterable of ApprovalRequest objects to process as a batch.

The important thing to remember is the priority of the values specified in these three mechanisms:

value in body takes precedence before any other

value in message header takes precedence before template value

value in template is set if no other value in header or body was given

For example to send one record for approval using values in headers use:

Given a route:

You could send a record for approval using:

Using Salesforce Recent Items API

To fetch the recent items use salesforce:recent operation. This operation returns an java.util.List of org.apache.camel.component.salesforce.api.dto.RecentItem objects (List<RecentItem>) that in turn contain the Id, Name and Attributes (with type and url properties). You can limit the number of returned items by specifying limit parameter set to maximum number of records to return.

For example:

Using Salesforce Composite API to submit SObject tree

To create up to 200 records including parent-child relationships use salesforce:composite-tree operation. This requires an instance of org.apache.camel.component.salesforce.api.dto.composite.SObjectTree in the input message and returns the same tree of objects in the output message. The org.apache.camel.component.salesforce.api.dto.AbstractSObjectBase instances within the tree get updated with the identifier values (Id property) or their corresponding org.apache.camel.component.salesforce.api.dto.composite.SObjectNode is populated with errors on failure.

Note that for some records operation can succeed and for some it can fail — so you need to manually check for errors.

Easiest way to use this functionality is to use the DTOs generated by the camel-salesforce-maven-plugin, but you also have the option of customizing the references that identify the each object in the tree, for instance primary keys from your database.

Lets look at an example:

Using Salesforce Composite API to submit multiple requests in a batch

The Composite API batch operation (composite-batch) allows you to accumulate multiple requests in a batch and then submit them in one go, saving the round trip cost of multiple individual requests. Each response is then received in a list of responses with the order perserved, so that the n-th requests response is in the n-th place of the response.

The results can vary from API to API so the result of the request is given as a java.lang.Object. In most cases the result will be a java.util.Map with string keys and values or other java.util.Map as value. Requests made in JSON format hold some type information (i.e. it is known what values are strings and what values are numbers), so in general those will be more type friendly. Note that the responses will vary between XML and JSON, this is due to the responses from Salesforce API being different. So be careful if you switch between formats without changing the response handling code.

Directory where to place generated DTOs, defaults to ${project.build.directory}/generated-sources/camel-salesforce

includes

List of SObject types to include

excludes

List of SObject types to exclude

includePattern

Java RegEx for SObject types to include

excludePattern

Java RegEx for SObject types to exclude

packageName

Java package name for generated DTOs, defaults to org.apache.camel.salesforce.dto.

For obvious security reasons it is recommended that the clientId, clientSecret, userName and password fields be not set in the pom.xml. The plugin should be configured for the rest of the properties, and can be executed using the following command:

The generated DTOs use Jackson and XStream annotations. All Salesforce field types are supported. Date and time fields are mapped to Joda DateTime, and picklist fields are mapped to generated Java Enumerations.