Sub topic 2
Defining a platform event and its fields
A platform event definition is the schema for a message. Publishers and subscribers both depend on it, so it comes before any publishing or subscribing code. This chapter assumes you know Salesforce custom objects and fields, and basic Apex syntax.
What a platform event definition is
A platform event is defined much like a custom object. The differences matter, though. An event is a message that passes through the event bus. It is not a record stored in a table you can edit later.
Because of this, an event definition has these traits:
- Its API name ends in
__e, not__c. An event namedOrder Shippedgets the API nameOrder_Shipped__e. - Published events are immutable. You cannot update or delete them.
- It has no page layouts, tabs, list views, or reports.
- Publishing and subscribing are governed by object permissions. A user or process needs Create access to publish and Read access to subscribe.
Create the event
In Setup, enter Platform Events in Quick Find and select New Platform Event. Fill in these values:
| Setting | Example | Notes |
|---|---|---|
| Label | Order Shipped | Shown in Setup and in flows. |
| Plural Label | Order Shipped Events | Required. |
| Object Name | Order_Shipped | Salesforce adds `__e`. |
| Publish Behavior | Publish After Commit | Covered in the next chapter. |
Choose the publish behavior deliberately. It controls when an event leaves the transaction, so it affects how publishers and subscribers behave.
Add custom fields
Open the event and add fields in the Custom Fields & Relationships section. The set of field types is smaller than for custom objects.
| Field type | Typical use |
|---|---|
| Text | Order number, status code |
| Text Area and Text Area (Long) | Longer messages |
| Number | Quantity, amount |
| Checkbox | Flags such as `Is_Priority__c` |
| Date and Date/Time | Ship date, timestamps |
Custom fields use the usual __c suffix. For this tutorial, create two Text fields on Order_Shipped__e:
Order_Number__cStatus__c
You can mark a field as required or give it a default value. Both are enforced when an event is published, so a missing required value makes the publish fail.
Standard fields
Every event also has system fields that Salesforce sets for you.
ReplayIdidentifies the event's position in the stream. It is how subscribers resume, and you will use it in later chapters.EventUuidis a unique identifier for the event message.CreatedDateandCreatedByIdrecord when and by whom it was published.
You cannot set these fields yourself.
Use the definition in Apex
Once deployed, the event is a type in Apex. You construct it like an sObject and fill in fields by API name.
Order_Shipped__e evt = new Order_Shipped__e(
Order_Number__c = '00012345',
Status__c = 'Shipped'
);
System.debug(evt.Order_Number__c);This code only builds the event in memory. Nothing is sent until you call EventBus.publish, which the "Publishing events from Apex" chapter covers.
Check your work
Before moving on, confirm three things in Setup:
- The event shows API name
Order_Shipped__e. - Both custom fields appear with
__cnames. - The profile or permission set you will use has Create and Read access to the event.
Exercise
You create a platform event with the Object Name "Invoice_Paid". What is its API name, and why can't a subscriber correct a wrong value by updating the published event?
Show answer
The API name is InvoicePaid_e. Platform events are immutable messages on the event bus, not editable records, so a published event cannot be updated or deleted. To correct a mistake, publish a new event.