此内容没有您所选择的语言版本。
Chapter 126. Salesforce
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:
<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-salesforce</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency>
Camel on EAP deployment
This component is supported by the Camel on EAP (Wildfly Camel) framework, which offers a simplified deployment model on the Red Hat JBoss Enterprise Application Platform (JBoss EAP) container. For details of this model, see chapter "Apache Camel on Red Hat JBoss EAP" in "Deploying into a Web Server".
URI format
The URI scheme for a salesforce component is as follows
salesforce:topic?options
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.
Rest API
getVersions
- Gets supported Salesforce REST API versionsgetResources
- Gets available Salesforce REST Resource endpointsgetGlobalObjects
- Gets metadata for all available SObject typesgetBasicInfo
- Gets basic metadata for a specific SObject typegetDescription
- Gets comprehensive metadata for a specific SObject typegetSObject
- Gets an SObject using its Salesforce IdcreateSObject
- Creates an SObjectupdateSObject
- Updates an SObject using IddeleteSObject
- Deletes an SObject using IdgetSObjectWithId
- Gets an SObject using an external (user defined) id fieldupsertSObject
- Updates or inserts an SObject using an external iddeleteSObjectWithId
- Deletes an SObject using an external idquery
- Runs a Salesforce SOQL queryqueryMore
- Retrieves more results (in case of large number of results) using result link returned from the 'query' APIsearch
- Runs a Salesforce SOSL query
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.
...to("salesforce:upsertSObject?sObjectIdName=Name")...
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 JobgetJob
- Gets a Job using its Salesforce IdcloseJob
- Closes a JobabortJob
- Aborts a JobcreateBatch
- Submits a Batch within a Bulk JobgetBatch
- Gets a Batch using IdgetAllBatches
- Gets all Batches for a Bulk Job IdgetRequest
- Gets Request data (XML/CSV) for a BatchgetResults
- Gets the results of the Batch when its completecreateBatchQuery
- Creates a Batch from an SOQL querygetQueryResultIds
- Gets a list of Result Ids for a Batch QuerygetQueryResult
- 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.
...to("salesforce:createBatchJob")..
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
from("salesforce:CamelTestTopic?notifyForFields=ALL¬ifyForOperations=ALL&sObjectName=Merchandise__c&updateTopic=true&sObjectQuery=SELECT Id, Name FROM Merchandise__c")...
To subscribe to an existing topic
from("salesforce:CamelTestTopic&sObjectName=Merchandise__c")...
Uploading a document to a ContentWorkspace
Create the ContentVersion in Java, using a Processor instance:
public class ContentProcessor implements Processor { public void process(Exchange exchange) throws Exception { Message message = exchange.getIn(); ContentVersion cv = new ContentVersion(); ContentWorkspace cw = getWorkspace(exchange); cv.setFirstPublishLocationId(cw.getId()); cv.setTitle("test document"); cv.setPathOnClient("test_doc.html"); byte[] document = message.getBody(byte[].class); ObjectMapper mapper = new ObjectMapper(); String enc = mapper.convertValue(document, String.class); cv.setVersionDataUrl(enc); message.setBody(cv); } protected ContentWorkspace getWorkSpace(Exchange exchange) { // Look up the content workspace somehow, maybe use enrich() to add it to a // header that can be extracted here .... } }
Give the output from the processor to the Salesforce component:
from("file:///home/camel/library") .to(new ContentProcessor()) // convert bytes from the file into a ContentVersion SObject // for the salesforce component .to("salesforce:createSObject");
Camel Salesforce Maven Plugin
This Maven plug-in generates DTOs for the Camel Salesforce.
Usage
The plug-in configuration has the following properties.
Option | Description |
---|---|
clientId
|
Salesforce client Id for Remote API access. |
clientSecret
|
Salesforce client secret for Remote API access. |
userName
|
Salesforce account user name. |
password
|
Salesforce account password (including secret token). |
version
|
Salesforce Rest API version, defaults to 25.0. |
outputDirectory
|
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 .
|
mvn camel-salesforce:generate -DclientId=<clientid> -DclientSecret=<clientsecret> -DuserName=<username> -Dpassword=<password>
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.