Sub topic 12
Writing back to the external system
This chapter assumes you can query external objects with SOQL and Apex, and that you have an external data source and at least one external object from Validate and Sync. Basic Apex syntax helps.
By default, external objects are read-only. Salesforce Connect can also send creates, updates, and deletes to the external system. This works only when the adapter and the external system both support writes.
Enabling writes
Writes are turned on in two places, and both must allow them.
- The external data source. Open the data source in Setup and select Writable External Objects. Until you do this, the external objects built on it reject DML (data manipulation language: insert, update, delete).
- The external system. The OData service or the other org must accept the operation. For OData, the service must expose create, update, and delete operations for that entity set. The user's credentials must also be allowed to perform them there.
Custom adapters from the Apex Connector Framework add a third condition. The provider must declare the capabilities ROW_CREATE, ROW_UPDATE, or ROW_DELETE, and the connection class must implement upsertRows() or deleteRows().
How a write differs from a normal save
A normal insert on a custom object runs inside the Salesforce transaction and is rolled back if something later fails. An external write is a callout to another system, and Salesforce cannot roll it back.
Salesforce supports two styles of external write:
- Immediate. The write goes to the external system right away, and you get the result in the same call. Use
Database.insertImmediate,updateImmediate, anddeleteImmediate. - Asynchronous. The write is queued and sent later. Use
Database.insertAsync,updateAsync, anddeleteAsync. These return a result with a state of "pending" until the system finishes.
The standard insert statement on an external object behaves like the asynchronous style. Use the immediate methods when you need to know the outcome right now.
Example: an immediate insert
Product__x p = new Product__x(Name__c = 'Widget', Price__c = 25);
Database.SaveResult sr = Database.insertImmediate(p);
if (!sr.isSuccess()) {
System.debug(sr.getErrors()[0].getMessage());
}insertImmediate sends the row to the external system and returns a SaveResult. If isSuccess() is false, the error message usually comes from the external system, so read it there first when debugging.
The external system assigns the ExternalId of a new row. After a successful insert, sr.getId() returns the new ID, which you can use to read the row back.
How the flow works
Common errors
- "Entity is read-only" or a similar DML error. Writable External Objects is not selected on the data source, or the permission set grants only Read on the object.
- Field not writable. Some external fields are read-only in the external schema, such as server-generated fields. Writing to them fails.
- Insert appears to succeed, but the row is missing. An asynchronous write has not finished or has failed later. Check the result, or use an immediate method.
- Callout limits. Each write counts as a callout. Bulk writes to many rows can hit the limits covered in the next chapters.
- Unexpected rollback behavior. If your Apex fails after an external write succeeded, the external change stays. Design for that, for example by making external writes the last step.
Exercise
A developer calls insert on an external object, and the call returns without errors. Ten seconds later the row is not in the external system. What are two possible explanations, and which method would give a result immediately?
Show answer
The standard insert on an external object is asynchronous, so the write may still be pending or may have failed after the call returned. The data source may also lack Writable External Objects, or the external system may have rejected the row. Database.insertImmediate sends the write right away and returns a SaveResult that reports success or failure.