# How do PushTopics combine SOQL query criteria with real-time streaming notifications in Salesforce?

**Topic:** Integration · **Difficulty:** Medium · **Tags:** Integration, PushTopics, SOQL Streaming

Read it on the site: https://www.forcechallenges.com/interview-questions/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:**
1. Insert a `PushTopic` record with `Name`, `Query`, `ApiVersion`, and the `NotifyForOperationCreate/Update/Delete/Undelete` flags.
2. `NotifyForFields` decides when updates fire: `Referenced`, `Select`, `Where` or `All`.
3. A client subscribes to `/topic/<Name>` with CometD or `lightning/empApi`.
4. When a record change matches the query, a message with the record fields from the `SELECT` list 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:**
```apex
PushTopic pt = new PushTopic();
pt.Name = 'BigOpportunities';
pt.Query = 'SELECT Id, Name, Amount, StageName FROM Opportunity WHERE Amount > 100000';
pt.ApiVersion = 62.0;
pt.NotifyForOperationCreate = true;
pt.NotifyForOperationUpdate = true;
pt.NotifyForFields = 'Referenced';   // fire when any field in SELECT or WHERE changes
insert pt;
// Subscribe to channel: /topic/BigOpportunities
```

**Hidden gotchas & must-remember points:**
- It does not notify on records that merely stop matching the query, so you can miss "left the filter" changes.
- `NotifyForFields = Referenced` is the default and often what you want. `All` is noisy.
- Not all objects are supported, and the PushTopic itself needs permission to create.
- Retention for replay is 24 hours.
- Messages carry only the selected fields, so the SELECT list defines your payload.
- Prefer CDC for new work.

**Best practice:** Use PushTopics only for small, filtered notification cases, and choose CDC for data synchronization.

---
Source: ForceChallenges · last updated 2026-11-02
