Sub topic 12
Custom metadata types, external objects, big objects and platform events
Custom objects are not the only custom entity type in Salesforce. Four other types cover jobs that a regular custom object does poorly: configuration data, data stored outside Salesforce, very large data sets, and event messages. This chapter assumes you know how custom objects and API names work, including the __c suffix.
The four types and their suffixes
Each type has its own API name suffix. The suffix tells you what kind of entity you are looking at, and it controls how you can query and write to it.
| Type | Suffix | Stores | Typical use |
|---|---|---|---|
| Custom metadata type | `__mdt` | Configuration records that deploy as metadata | Settings, mappings, feature switches |
| External object | `__x` | Pointers to rows in an outside system | Showing ERP or database data without copying it |
| Big object | `__b` | Billions of records | Audit trails, history, archives |
| Platform event | `__e` | Messages, not stored rows | Notifying other systems or components |
Custom metadata types
A custom metadata type works like a custom object whose records are metadata. The type is the definition, with its fields. Each record is a row of configuration, such as one row per country holding a tax rate.
Because the records are metadata, they move with the type in a deployment. You do not need a data load to copy them to another org. Apex can read them without counting against SOQL query limits.
SELECT DeveloperName, Tax_Rate__c
FROM Tax_Setting__mdt
WHERE DeveloperName = 'Germany'Note that the fields on a custom metadata type still end in __c. Only the type itself ends in __mdt. Use this type when the data is configuration that developers or admins maintain, not business data that users create.
External objects
An external object maps to a table in a system outside Salesforce. Salesforce reads the data when a user opens a record or runs a query. It does not copy the data into the org.
An external data source supplies the connection. This can be a Salesforce Connect adapter using OData or a custom adapter written in Apex. Relationships work differently here. Lookups to external records use special types such as the external lookup and indirect lookup.
Choose an external object when the data must stay in its source system or is too large or too volatile to replicate. Expect slower access, because each read goes over the network, and fewer features than a standard custom object.
Big objects
A big object stores very large volumes of data, in the range of millions to billions of records, on a different storage layer from regular objects. The trade-off is limited functionality:
- You define an index when you create the object. Queries must filter on the index fields in the order they were defined.
- Triggers, flows, and standard reports are not supported in the usual way.
- You write records with
Database.insertImmediate()in Apex, not with a regularinsert.
Use a big object to archive records you rarely read but must keep, such as years of login history.
Platform events
A platform event is a message that one part of a system publishes and other parts subscribe to. Publishing does not create a record you can browse in the same way as a custom object row. Subscribers include Apex triggers, flows, and external systems.
Order_Shipped__e evt = new Order_Shipped__e(
Order_Number__c = 'ORD-1001'
);
Database.SaveResult result = EventBus.publish(evt);
System.debug('Published: ' + result.isSuccess());This code assumes a platform event named Order_Shipped__e with a text field Order_Number__c. Use events to decouple processes, for example to tell a billing system that an order shipped.
Choosing among them
Common mistakes
- Using a custom object for settings that should deploy with your code. A custom metadata type avoids a separate data load.
- Copying external data into Salesforce when an external object would do. You end up with stale duplicates.
- Treating a big object like a normal object. Queries that skip the index fields fail.
- Expecting a platform event to behave like a stored record. It is a message, and subscribers must be ready to receive it.
Exercise
Your team needs a table of shipping rates per region. Admins edit the rates, and the rates must deploy from a sandbox to production with the code. Which entity type fits, and what suffix does its API name end in?
Show answer
A custom metadata type, whose API name ends in __mdt. Its records are metadata, so they deploy with the type and Apex can read them without using SOQL query limits.
Exercise
You must keep ten years of audit entries, with billions of rows, and you rarely read them. Which type fits, and how do you write records to it from Apex?
Show answer
A big object (__b). Write records with Database.insertImmediate(), and design the index so that your queries filter on the index fields in order.