How do PushTopics combine SOQL query criteria with real-time streaming notifications in Salesforce?
Answer
Basics: Instead of polling for changes, a client subscribes to a PushTopic channel and receives JSON messages for records that meet the criteria, such as Opportunities over a certain amount.
How it works:
- Insert a
PushTopicrecord withName,Query,ApiVersion, and theNotifyForOperationCreate/Update/Delete/Undeleteflags. NotifyForFieldsdecides when updates fire:Referenced,Select,WhereorAll.- A client subscribes to
/topic/<Name>with CometD orlightning/empApi. - When a record change matches the query, a message with the record fields from the
SELECTlist is sent.
Advanced / deep dive: Query restrictions are strict. The SELECT must include Id, the query is on one object, and features such as LIMIT, ORDER BY, GROUP BY, aggregates and relationship fields are not supported (verify the exact list). The platform checks the subscriber's object and field-level access. Events are retained for 24 hours for replay (platform events and change events keep 72), and there are per-org limits on the number of PushTopics and on concurrent clients (verify the current numbers for your edition). Generic Streaming (StreamingChannel) is a separate mechanism for custom messages. Newer designs use CDC because it covers many more objects, includes changed-field detail and has longer retention.
Code / config example:
();
pt.Name = ;
pt.Query = ;
pt.ApiVersion = ;
pt.NotifyForOperationCreate = ;
pt.NotifyForOperationUpdate = ;
pt.NotifyForFields = ;
insert pt;