platform-events-apex

Use when publishing or subscribing to Salesforce Platform Events from Apex, comparing Platform Events with Change Data Capture, or designing event-triggered error handling and monitoring. Triggers: 'EventBus.publish', 'platform event trigger', 'CDC vs Platform Events', 'replay ID', 'high-volume event'. NOT for Flow-only publish/subscribe automation.

Best use case

platform-events-apex is best used when you need a repeatable AI agent workflow instead of a one-off prompt.

Use when publishing or subscribing to Salesforce Platform Events from Apex, comparing Platform Events with Change Data Capture, or designing event-triggered error handling and monitoring. Triggers: 'EventBus.publish', 'platform event trigger', 'CDC vs Platform Events', 'replay ID', 'high-volume event'. NOT for Flow-only publish/subscribe automation.

Teams using platform-events-apex should expect a more consistent output, faster repeated execution, less prompt rewriting.

When to use this skill

  • You want a reusable workflow that can be run more than once with consistent structure.

When not to use this skill

  • You only need a quick one-off answer and do not need a reusable workflow.
  • You cannot install or maintain the underlying files, dependencies, or repository context.

Installation

Claude Code / Cursor / Codex

$curl -o ~/.claude/skills/platform-events-apex/SKILL.md --create-dirs "https://raw.githubusercontent.com/PranavNagrecha/AwesomeSalesforceSkills/main/skills/apex/platform-events-apex/SKILL.md"

Manual Installation

  1. Download SKILL.md from GitHub
  2. Place it in .claude/skills/platform-events-apex/SKILL.md inside your project
  3. Restart your AI agent — it will auto-discover the skill

How platform-events-apex Compares

Feature / Agentplatform-events-apexStandard Approach
Platform SupportNot specifiedLimited / Varies
Context Awareness High Baseline
Installation ComplexityUnknownN/A

Frequently Asked Questions

What does this skill do?

Use when publishing or subscribing to Salesforce Platform Events from Apex, comparing Platform Events with Change Data Capture, or designing event-triggered error handling and monitoring. Triggers: 'EventBus.publish', 'platform event trigger', 'CDC vs Platform Events', 'replay ID', 'high-volume event'. NOT for Flow-only publish/subscribe automation.

Where can I find the source code?

You can find the source code on GitHub using the link provided at the top of the page.

SKILL.md Source

Use this skill when a design is moving toward event-driven integration and Apex is involved on the publishing or subscriber side. The goal is to publish events deliberately, consume them in a decoupled way, and separate Platform Events from Change Data Capture instead of treating them as interchangeable.

## Before Starting

- Is the system broadcasting a business event it owns, or mirroring a record change that Salesforce owns already?
- Will the subscriber be Apex, Flow, middleware, Pub/Sub API, or a mix?
- Do you need eventual consistency, replay handling for external consumers, or immediate transaction-level validation?

## Core Concepts

### Platform Events Are Explicit Business Messages

Platform Events are event records designed for decoupled publication and subscription. In Apex, publishers create event instances and call `EventBus.publish`. That is different from CDC, where Salesforce emits change events when records mutate. Use Platform Events when you want to define the payload and timing of the message instead of mirroring all record changes automatically.

### Publication Success Is Not “No Exception Thrown”

Publishing can be done for one event or a list, and the return value matters. `EventBus.publish` can return `Database.SaveResult` style results that tell you whether publication succeeded. Production code should inspect those results or otherwise classify failures. Treating event publication as fire-and-forget with no result handling is an observability gap.

### Subscriber Shape Depends On The Consumer

Apex platform event triggers subscribe asynchronously and run only in an `after insert` context. External consumers using Streaming or Pub/Sub APIs care about replay positions and subscriber durability. Replay ID management is not an Apex-trigger concern, but it is part of the architecture whenever middleware or external apps must recover missed events.

### CDC Solves A Different Problem

Change Data Capture is best when the event should represent Salesforce row changes. Platform Events are better when the event is a business fact or orchestration signal such as “Order Approved” or “Invoice Sync Requested.” Mixing these without a decision rule creates noisy payloads and unclear ownership.

## Common Patterns

### Publish Via A Service Boundary

**When to use:** Business logic has decided that an event should be emitted.

**How it works:** Build the event payload in a service class, publish a list where possible, and inspect results for failure logging.

**Why not the alternative:** Inline event creation inside many triggers or controllers duplicates payload rules and weakens monitoring.

### Event Trigger To Queueable Worker

**When to use:** Subscriber logic needs callouts, retries, or heavier processing.

**How it works:** Keep the platform event trigger thin and hand off durable work to Queueable Apex.

### CDC Versus Platform Event Decision

**When to use:** Architects are unsure whether to publish a custom event or consume record-change events.

**How it works:** Use CDC when record mutation itself is the signal. Use Platform Events when the signal is business-defined and may not map one-to-one to DML.

## Decision Guidance

| Situation | Recommended Approach | Reason |
|---|---|---|
| Need to broadcast a domain event like “Application Submitted” | Platform Event | Explicit payload and publisher-controlled timing |
| Need downstream systems to react to Account or Contact data changes | CDC | Change stream already represents row changes |
| Apex subscriber needs callouts or heavier orchestration | Platform event trigger + Queueable | Thin trigger, safer processing boundary |
| External consumer must recover after downtime | Replay-aware external subscriber | Replay handling belongs with external consumer state |


## Recommended Workflow

Step-by-step instructions for an AI agent or practitioner activating this skill:

1. Gather context — confirm the org edition, relevant objects, and current configuration state
2. Review official sources — check the references in this skill's well-architected.md before making changes
3. Implement or advise — apply the patterns from Core Concepts and Common Patterns sections above
4. Validate — run the skill's checker script and verify against the Review Checklist below
5. Document — record any deviations from standard patterns and update the template if needed

---

## Review Checklist

- [ ] Publishers inspect publish results or log publication failures deliberately.
- [ ] Platform event triggers stay thin and avoid becoming integration god-classes.
- [ ] Event payloads represent business intent, not accidental object snapshots.
- [ ] CDC and Platform Events are chosen for clear, different reasons.
- [ ] External replay expectations are documented when non-Apex consumers exist.
- [ ] Monitoring includes event publication failures and subscriber failures.

## Salesforce-Specific Gotchas

1. **Apex platform event triggers are asynchronous `after insert` subscribers** — do not design them like normal object triggers.
2. **Replay IDs matter for external consumers, not as a substitute for Apex trigger state** — keep that responsibility in the consumer architecture.
3. **Publishing inside loops scales poorly** — collect events and publish in bulk.
4. **A successful transaction does not guarantee every subscriber succeeded** — subscriber monitoring must be independent.

## Output Artifacts

| Artifact | Description |
|---|---|
| Event decision guide | Recommendation for Platform Events vs CDC and publisher/subscriber boundaries |
| Publish/subscribe review | Findings on payload design, result handling, and consumer reliability |
| Event pattern scaffold | Sample `EventBus.publish` service and thin subscriber-trigger structure |

## Related Skills

- `apex/async-apex` — use when the subscriber workload really needs Queueable or Batch design rather than event design alone.
- `apex/exception-handling` — use when publication failures or subscriber exceptions need better classification and logging.
- `apex/callouts-and-http-integrations` — use when event consumers ultimately call external HTTP systems.

Related Skills

apex-managed-sharing-patterns

8
from PranavNagrecha/AwesomeSalesforceSkills

Grant row-level access programmatically via __Share records when declarative sharing rules cannot express the policy. NOT for OWD, role hierarchy, or criteria-based sharing rule design.

lwc-server-sent-events

8
from PranavNagrecha/AwesomeSalesforceSkills

Use when building LWCs that must react to live server pushes — Platform Events, Change Data Capture, or streaming updates — via the lightning/empApi (CometD) subscription model. Covers lifecycle, replayId, error handling, reconnection, scale considerations, and multi-tab behavior. Does NOT cover publishing events (see platform-events or apex-platform-events).

lwc-imperative-apex

8
from PranavNagrecha/AwesomeSalesforceSkills

Call Apex methods imperatively from LWC — on button click, lifecycle hooks, or conditional logic. Covers import syntax, cacheable vs non-cacheable, async/await patterns, error handling, loading states, and Promise.all. NOT for wire service (use wire-service-patterns) and NOT for testing Apex mocks (use lwc-testing).

platform-events-integration

8
from PranavNagrecha/AwesomeSalesforceSkills

Use when publishing Platform Events from external systems via REST API, subscribing to Platform Events from outside Salesforce via CometD or Pub/Sub API, designing replay ID strategy for durable external consumers, or handling high-volume event delivery guarantees. Trigger keywords: 'external publish platform event', 'CometD subscribe', 'Pub/Sub API', 'replay ID external', 'durable subscription', 'RetainUntilDate'. NOT for Apex-only event publishing or triggering (use platform-events-apex). NOT for Change Data Capture external subscription (use change-data-capture-integration).

platform-event-schema-evolution

8
from PranavNagrecha/AwesomeSalesforceSkills

Use when modifying the schema of a Platform Event that already has live publishers and subscribers — adding fields, deprecating fields, or splitting events. Triggers: 'add field to platform event without breaking subscribers', 'platform event versioning', 'evolve event schema safely', 'rename a field on a published event'. NOT for initial event design (use integration/platform-events-integration) or for Change Data Capture event schemas.

platform-event-publish-patterns

8
from PranavNagrecha/AwesomeSalesforceSkills

Publishing Platform Events: EventBus.publish, PublishBehavior (PublishImmediately vs PublishAfterCommit), high-volume events, event allocation, publish failures, Change Data Capture comparison. NOT for subscribing/consuming (use platform-event-subscribe-patterns). NOT for CDC architecture (use cdc-patterns).

dataweave-for-apex

8
from PranavNagrecha/AwesomeSalesforceSkills

Use when transforming structured data inside Apex — CSV → JSON, XML → SObject list, JSON → flattened CSV, or schema-mapping a third-party payload to a Salesforce model — and the existing options (`JSON.deserialize`, `Dom.Document`, hand-written loops) are getting unwieldy. Triggers: 'apex transform csv json xml without external library', 'system.dataweave script', 'salesforce native dataweave apex execute', 'transform xml to sobject apex no mulesoft', 'json reshape salesforce apex script'. NOT for MuleSoft Anypoint DataWeave running off-platform (use mulesoft-anypoint-architecture), NOT for Apex JSON serialization basics (use apex-json-serialization), NOT for Bulk API CSV ingest (use bulk-api-2-patterns).

pause-elements-and-wait-events

8
from PranavNagrecha/AwesomeSalesforceSkills

Use this skill when designing or troubleshooting flows that need to suspend execution and wait for an external signal — either a time-based alarm or a platform event. Trigger keywords: pause element, wait element, flow interview suspension, alarm event, platform event resume, async interview. NOT for scheduled flows that recur on a fixed schedule (use scheduled-flows), and NOT for record-triggered flow scheduled paths on a single record (those are set in the Start element, not a Pause element).

flow-platform-events-integration

8
from PranavNagrecha/AwesomeSalesforceSkills

Use when Flow is a Platform Event publisher, subscriber, or both. Triggers: 'publish platform event from flow', 'platform-event-triggered flow', 'high-volume platform event', 'publish after commit vs immediate', 'PE subscriber error handling', 'integration fan-out from save'. NOT for Change Data Capture (see integration skills) or for generic async work that does not need pub/sub (see flow/scheduled-flows).

flow-invocable-from-apex

8
from PranavNagrecha/AwesomeSalesforceSkills

Author @InvocableMethod Apex classes that Flow can call as Actions. Design the input / output variable contract, bulk semantics (one list in, one list out), null handling, and error surfacing. Also covers the inverse direction: calling a flow from Apex via Flow.Interview. NOT for general Apex authoring (use apex-service-selector-domain). NOT for REST-exposed Apex (use apex-rest-resource-patterns).

flow-apex-defined-types

8
from PranavNagrecha/AwesomeSalesforceSkills

Design and use Apex-Defined Types as Flow variables for structured non-sObject data (HTTP callout payloads, External Service responses, complex configuration). Trigger keywords: apex-defined type, flow variable, @AuraEnabled class, flow http callout response. Does NOT cover building HTTP Callout Actions themselves, External Services schema, or raw Apex invocable methods.

flow-and-platform-events

8
from PranavNagrecha/AwesomeSalesforceSkills

Publish and subscribe to Platform Events from Flow for async decoupling, high-volume triggers, and cross-org signaling. NOT for regular DML-triggered flows.