Sub topic 12
Security and field-level access
This chapter assumes you know the Salesforce permission model (profiles, permission sets, object permissions, field-level security) and that you've read the earlier chapters on change event structure, triggers, and subscribers. Here you'll see which security rules apply to a change event, and where they stop applying.
Who receives which fields
A change event reaches a subscriber only if that subscriber's user can read the object. The payload is then filtered by field-level security. If the user can't read Phone, the Phone value is left out of the event, even when it changed.
The ChangeEventHeader is always included. It exposes the entity name, record IDs, change type, and the list of changed field names. Consider this when the field names themselves are sensitive.
| Consumer | Runs as | Field filtering |
|---|---|---|
| Lightning web component using empApi | The logged-in user | Filtered by that user's access |
| External client (CometD or Pub/Sub API) | The user that authenticated | Filtered by that user's access |
| Apex change event trigger | The Automated Process user | Not filtered by an end user's access |
Triggers see more than users do
An Apex change event trigger doesn't run as the person who edited the record. It runs as the Automated Process user. The trigger can therefore read every field in the event, and any DML it performs uses that user's access.
The header's commitUser field still identifies who made the original change. If your logic depends on that person's permissions, check it explicitly. Don't assume the trigger inherits it.
trigger AccountChangeTrigger on AccountChangeEvent (after insert) {
for (AccountChangeEvent event : Trigger.new) {
EventBus.ChangeEventHeader header = event.ChangeEventHeader;
// The trigger runs as Automated Process.
// commitUser is the user who made the change.
System.debug('Changed by ' + header.commitUser);
}
}Design guidance
- Give integration users minimal access. Create a dedicated user for each external subscriber. Grant read access only to the objects and fields it needs. Missing fields will silently disappear from events, so test with the real integration user, not an administrator.
- Don't count on record sharing to limit delivery. Plan around object and field permissions. If different subscribers must see different records, filter downstream, or confirm the current behavior in the Salesforce documentation before relying on sharing.
- Be careful where events go. A change event is a copy of record data. Once an external system receives it, Salesforce permissions no longer protect it. Store and forward only what you need.
- Review the data in logs. The empApi example earlier logs only the change type and record IDs. Logging full payloads can spread sensitive values.
Common errors
- Testing as an administrator. The subscriber works in the sandbox, then fails in production because the integration user can't read a field.
- Assuming a trigger respects the editor's access. The Automated Process user reads fields the editing user couldn't.
- Treating an empty field as "unchanged". A field is absent from the payload if it didn't change or if the subscriber can't read it. Use
changedFieldsin the header to tell which fields changed, but remember it lists names only.
Exercise
An external subscriber receives an Account update event whose header lists Phone in changedFields, but the payload has no Phone value. What is the most likely cause, and how would you confirm it?
Show answer
The integration user most likely lacks field-level read access to Phone, so Salesforce removed the value from that user's copy of the event. Confirm by checking the user's profile and permission sets for read access to Account.Phone, then grant it if the integration needs the field.