Context
You use Salesforce to track leads, manage your pipeline, create opportunities, and capture order details that convert leads to customers. But, Salesforce isn’t the system that contains or processes orders. Orders are managed by an external (remote) system. That remote system needs to update the order status in Salesforce as the order passes through its processing stages.
Problem
How does a remote system connect and authenticate with Salesforce to notify Salesforce about external events, create records, and update existing records?
Forces
- Is the purpose of the remote call to Salesforce to notify Salesforce of an event that occurred externally using an event-driven architecture? Or is the purpose to perform CRUD operations on specific records? If you use an event-driven architecture, the event producer (the remote process) is decoupled from the Salesforce event consumer.
- Does the call to Salesforce require the remote process to wait for a response before continuing processing? Remote calls to Salesforce are always synchronous request-reply, although the remote process can discard the response if it isn’t needed to simulate an asynchronous call.
- Does each transaction operate against a single Salesforce object or multiple, related objects?
- What is the format of the message (for example, SOAP or REST, or both over HTTP)?
- Is the message size relatively small or large?
- If the remote system is SOAP-capable, is the remote system able to participate in a contract-first approach, where Salesforce dictates the contract? This is required where our SOAP API is used, for which a predefined WSDL is supplied.
- Is transaction processing required?
- What is the extent to which you’re tolerant of customization in Salesforce?
Solution
| Solution | Fit | Comments |
|---|---|---|
| SOAP API | Best |
|
| REST API | Best |
|
| Apex web services | Suboptimal | Apex class methods can be exposed as web service methods to external applications.
This method is an alternative to SOAP API, and is typically used only where the following
additional requirements must be met.
The benefit of using an Apex web service must be weighed against the additional code that needs to be maintained in Salesforce. Not applicable for platform events because transaction pre-insert logic at the consumer doesn’t apply in an event-driven architecture. To notify a Salesforce org that an event has occurred, use SOAP API, REST API, or Bulk API 2.0. |
| Apex REST services | Suboptimal | An Apex class can be exposed as REST resources mapped to specific URIs with an HTTP
verb defined against it (for example, POST or GET). You can use REST API composite resources to perform multiple updates in a single transaction. Unlike SOAP, there’s no need for the client to consume a service definition/contract (WSDL) and generate client stubs. The remote system requires only the ability to form an HTTP request and process the returned results (XML or JSON). Not applicable for platform events because transaction pre-insert logic at the consumer doesn’t apply in an event-driven architecture. To notify a Salesforce org that an event has occurred, use SOAP API, REST API, or Bulk API 2.0. |
| Bulk API 2.0 | Optimal for bulk operations | Bulk API 2.0 is based on REST principles, and is optimized for loading or deleting
large sets of data. It has the same accessibility and security behavior as REST API. Any data operation that includes more than 2,000 records is a good candidate for Bulk API 2.0 to successfully prepare, execute, and manage an asynchronous workflow that uses the Bulk framework. Jobs with fewer than 2,000 records should involve “bulkified” synchronous calls in REST (for example, Composite) or SOAP. Bulk API 2.0 allows the client application to query, insert, update, upsert, or delete a large number of records asynchronously by submitting a number of batches, which are processed in the background by Salesforce. In contrast, SOAP API is optimized for real-time client applications that update small numbers of records at a time. Although SOAP API can also be used for processing large numbers of records, when the data sets contain hundreds of thousands to millions of records, it becomes less practical. This is due to its relatively high overhead and lower performance characteristics.
|
Sketch
The following diagrams illustrate the sequence of events when you implement this pattern using either REST API for notifications from external events or SOAP API to query a Salesforce object. The sequence of events is the same when using REST API.
Results
In an event-driven architecture, the remote system calls into Salesforce using SOAP API, REST API, or Bulk API 2.0 to publish an event to the Salesforce event bus. Publishing an event notifies all subscribers. Event subscribers can be on the Salesforce Platform such as Process Builder Processes, Flows, or Lightning Components, Apex triggers. Event subscribers can also be external to the Salesforce Platform such as CometD subscribers.
- The remote system to call the Salesforce APIs to query the database and execute single-object operations (create, update, delete, and so on).
- The remote system to call the Salesforce REST API composite resources to perform a series of object operations.
- Remote system to call custom-built Salesforce APIs (services) that can support multi-object transactional operations and custom pre/post processing logic.
| Calling mechanism | Description |
|---|---|
| SOAP API | The remote system uses the Salesforce Enterprise or Partner WSDL to generate client stubs that are in turn used to invoke the standard SOAP API. |
| REST API | The remote system has to authenticate before accessing any Apex REST service. The
remote system can use OAuth 2.0 or username and password authentication. In either case,
the client must set the authorization HTTP header with the appropriate value (an OAuth
access token or a session ID can be acquired via a login call to SOAP API). The remote system then generates REST invocations (HTTP requests) with the appropriate verbs and processes the results returned (JSON and XML data formats are supported). |
| Apex web service | The remote system consumes the custom Apex web service WSDL to generate client stubs that are in turn used to invoke the custom Apex web service. |
| Apex REST service | As per REST API, the resource URI and applicable verbs are defined using the @RestResource, @HttpGet, and @HttpPost annotations. |
| Bulk API 2.0 | Bulk API 2.0 is a REST-based API, so the same calling mechanisms as REST API apply. |
| REST API to invoke Flow | Use REST API to call a custom invocable actions endpoint to invoke an auto-launched flow. |
An error handling and recovery strategy must be considered as part of the overall solution.
- Error handling—All the remote call-in methods, standard or custom APIs, require the remote system to handle any subsequent errors, such as timeouts and the management of retries. Middleware can be used to provide the logic for error handling and recovery.
- Recovery—A custom retry mechanism needs to be created if quality-of-service requirements dictate it. In this case, it’s important to ensure idempotent design characteristics. Platform event enables subscribers to use the replay ID to fetch messages within a certain time period after those messages were published.
Idempotent Design Considerations
Idempotent capabilities guarantee that repeated invocations are safe and won’t have a negative effect. If idempotency isn’t implemented, then repeated invocations of the same message can have different results, potentially resulting in data integrity issues, for example, creation of duplicate records, duplicate processing of transactions, and so on.
The remote system must manage multiple (duplicate) calls, in the case of errors or timeouts, to avoid duplicate inserts and redundant updates (especially if downstream triggers and workflow rules fire). While it’s possible to manage some of these situations within Salesforce (particularly in the case of custom SOAP and REST services), we recommend that the remote system (or middleware) manages error handling and idempotent design.
| Solution | Security considerations |
|---|---|
| SOAP API | Salesforce supports Secure Sockets Layer v3 (SSL) and the Transport Layer Security
(TLS) protocols. Ciphers must have a key length of at least 128 bits. The remote system must log in using valid credentials to obtain a session ID. If the remote system already has a valid session ID, then it can call the API without an explicit login. This is used in call-back patterns covered earlier in this document, where a preceding Salesforce outbound message contained a session ID and record ID for subsequent use. We recommend that clients that call SOAP API cache and reuse the session ID to maximize performance, rather than obtaining a new session ID for each call. |
| REST API | We recommend that the remote system establish an OAuth trust for authorization. REST
calls can then be made on specific resources using HTTP verbs. It’s also possible to make
REST calls with a valid session ID that might have been obtained by other means (for
example, retrieved by calling SOAP API or provided via an outbound message). We recommend that clients that call the REST API cache and reuse the session ID to maximize performance, rather than obtaining a new session ID for each call. |
| Apex web service | We recommend that the remote system establish an OAuth trust for authorization. |
| Apex REST service | We recommend that the remote system establish an OAuth trust for authorization. |
| Bulk API 2.0 | We recommend that the remote system establish an OAuth trust for authorization. |
Sidebars
- Session timeout — The session times out if there’s no activity based on the Salesforce org’s session timeout setting.
- Query timeout — Each SOQL query has an individual timeout limit of 120 seconds.
| Solution | Communication type | Limits |
|---|---|---|
| SOAP API or REST API | Synchronous |
|
| Bulk API 2.0 | Synchronous |
Bulk API 2.0 is optimized for importing and exporting large sets of data asynchronously. Any data operation that includes more than 2,000 records is a good candidate for Bulk API 2.0 to successfully prepare, execute, and manage an asynchronous workflow that uses the Bulk framework. Jobs with fewer than 2,000 records should involve “bulkified” synchronous calls in REST (for example, Composite) or SOAP. Bulk API 2.0 is synchronous when submitting the batch request and associated data. The actual processing of the data occurs asynchronously. For more information about API and batch processing limits, see Limits. |
Endpoint Capability and Standards Support
The capability and standards support for the endpoint depends on the solution that you choose.
| Solution | Endpoint considerations |
|---|---|
| SOAP API | The remote system must be capable of implementing a client that can call the
Salesforce SOAP API, based on a message format predefined by Salesforce. The remote system (client) must participate in a contract-first implementation where the contract is supplied by Salesforce (for example, Enterprise or Partner WSDL). |
| REST API | The remote system must be capable of implementing a REST client that invokes Salesforce—defined REST services, and processes the XML or JSON results. |
| Apex web service | The remote system must be capable of implementing a client that can invoke SOAP
messages of a predefined format, as defined by Salesforce. The remote system must participate in a code-first implementation, where the contract is supplied by Salesforce after the Apex web service is implemented. Each Apex web service has its own WSDL. |
| Apex REST service | The same endpoint considerations as REST API apply. |
- Salesforce stores the remote system’s primary or unique surrogate key for the remote record.
- The remote system stores the Salesforce unique record ID or some other unique surrogate key.
There are specific considerations for handling integration keys in this synchronous pattern.
| Master system | Description |
|---|---|
| Salesforce | In this scenario, the remote system stores either the Salesforce RecordId or some other unique surrogate key from the record. |
| Remote system | In this scenario, Salesforce stores a reference to the unique identifier in the remote system. Because the process is synchronous, the key can be provided as part of the same transaction using external ID fields. |
| Solution | Considerations |
|---|---|
| SOAP API or REST API | SOAP API and REST API provide for simple transactions on objects. Complex integration scenarios, such as aggregation, orchestration, and transformation, can’t be performed in Salesforce. These scenarios must be handled by the remote system or middleware, with middleware as the preferred method. |
| Apex web service or Apex REST service | Custom web services can provide for cross-object functionality, custom logic, and more complex transaction support. This solution should be used with care, and you should always consider the suitability of middleware for any transformation, orchestration, and error handling logic. |
| Solution | Limits |
|---|---|
| SOAP API, REST API, and custom Apex APIs |
|
| Bulk API 2.0 | See Data Volumes sidebar for more information. |
| Platform Events |
|
Reliable messaging attempts to resolve the issue of guaranteeing the delivery of a message to a remote system where the individual components themselves might be unreliable. The Salesforce SOAP API and REST API are synchronous and don’t provide explicit support for any reliable messaging protocols, per se (for example, WS-ReliableMessaging).
We recommend that the remote system implement a reliable messaging system to ensure that error and timeout scenarios are successfully managed. Publishing of platform events from external systems relies on Salesforce APIs, so the same considerations for SOAP API and REST API apply.
This table highlights the desirable properties of a middleware system that participates in this pattern:
| Property | Mandatory | Desirable | Not required |
|---|---|---|---|
| Event handling | X | ||
| Protocol conversion | X | ||
| Translation and transformation | X | ||
| Queuing and buffering | X | ||
| Synchronous transport protocols | X | ||
| Asynchronous transport protocols | X | ||
| Mediation routing | X | ||
| Process choreography and service orchestration | X | ||
| Transactionality (encryption, signing, reliable delivery, and transaction management) | X | ||
| Routing | X | ||
| Extract, transform, and load | X (for bulk/batches) |
Example
A printing supplies and services company uses Salesforce as a front end to create and manage printer supplies and orders. Salesforce asset records representing printers are periodically updated with printing usage statistics (ink status, paper level) from the on-premises Printer Management System (PMS), which regularly monitors printers on client sites. Upon reaching a set threshold value (for example, low ink status or low/empty paper level of less than 30%), multiple apps/processes (variable) interested in the event are notified, email or Chatter alerts are sent, and an Order record is created. The PMS stores the Salesforce ID (Salesforce is the asset record master).
-
The PMS can participate in a contract-first integration, where Salesforce provides the contract and the PMS acts as a client (consumer) of the Salesforce service (defined via the Enterprise or Partner WSDL).
-
There should be no custom development in Salesforce.
This example is best implemented using the Salesforce SOAP API or REST API to publish events, and declarative automation (Process Builder) in Salesforce. The primary reason to use platform events is the variable and non-finite number of subscribers; however, for simple updates to a finite list of records such as orders, then use SOAP or REST API to update the records.
- Define a platform event in Salesforce to contain the notification data coming from the PMS.
- Create a Process Builder process that’s triggered by the printer event notification. The process updates the printer asset and creates an order (using an auto-launched Flow).
- Download the Enterprise or Partner WSDL and provide it to the remote system.
-
Create a client stub from the Enterprise or Partner WSDL.
- Authenticate to Salesforce (via OAuth web server or bearer token flow) using the integration user’s credentials.
- Upon printer status event, the PMS calls the API to create the printer status platform event (with printer usage statistics). The Salesforce event bus notifies Process Builder subscriber and all other subscribers.
When you use platform events, the event bus lets you replay events for 72 hours for high-volume platform events. Publishing those events using a middleware solution can help incorporate error handling on the publishing side. However, you can implement error handling on the subscribing side if you need higher reliability.
-
Implementation of a Salesforce synchronous API client (consumer).
-
A callback to Salesforce to publish a platform event (aligned with previously covered request/reply platform event patterns).