PushTopic events in Salesforce provide a secure and scalable way to receive notifications for changes to Salesforce data that match a SOQL query you define. These events can be handled within Salesforce or outside Salesforce. Push technology, also called the publish/subscribe model, transfers information that is initiated from a server to the client. This type of communication is the opposite of pull technology in which a request for information is made from a client to the server.
What is Streaming API?
It enables the transmission of real-time data from the Salesforce Platform. Data is transmitted using Push Technology. Push Technology transfers information that is initiated from a server to the client. You can use one of the subscription mechanisms such as CometD, and Subscription API in node js or in Mulesoft. Push Technology is also called the publish/subscribe Model. CometD enables the server to push data to the client whenever it is available while the client maintains a connection to the server.
Streaming API and Event Products
- Push Topic: Stream Salesforce changes based on SOQL
- Generic Streaming Events: publish Arbitrary String value
- Change Data Capture: New Version of Push Topic. No to SOQL
- Platform Event: new Version of Generic Streaming.
What are PushTopic Events in Salesforce?
Push Topic enables to stream Salesforce record changes to clients based on criteria you define in
a SOQL query in a PushTopic record.
When to Use PushTopic in Salesforce?
- Receive notifications of Salesforce record changes, including create, update, delete and undelete operations.
- Capture changes for the fields and records that match a SOQL query. Eg: Send changes when the opportunity is closed.
- Receive change notifications for only the records a user has access to based on sharing rules.
How to Create PushTopic
Follow the below steps to create a PushTopic:-
- Create a PushTopic based on a SOQL query. That will define the channel.
- Clients subscribe to the channel.
- When any CRUD operation happened on a record. The changes to that record are evaluated.
- If the record changes match the criteria of the PushTopic query, a notification is generated by the server and received by the subscribed clients.
Here is sample code for PushTopic
PushTopic pushTopic = new PushTopic(); pushTopic.Name = ‘ClosedCase’; pushTopic.Query = 'SELECT Id, Name, Subject, Status from Case where status = closed'; pushTopic.ApiVersion = 56.0; pushTopic.NotifyForOperationCreate = true; pushTopic.NotifyForOperationUpdate = true; pushTopic.NotifyForOperationUndelete = true; pushTopic.NotifyForOperationDelete = true; pushTopic.NotifyForFields = 'Referenced'; insert pushTopic;
- Please check examples in NotifyForFields : https://developer.salesforce.com/docs/atlas.en-us.api_streaming.meta/api_streaming/notifyforfields_where.htm
- Remember Large Text Area is not supported in the query
Points to Remember
- Push Topic is an object in salesforce.
- Name is important as we need to tell the client to use the name of the push topic.
- When we create a record then it is inserted in pushtopic.
Limitation of PushTopics in Salesforce
- There is a limit on the maximum number of Push Topics(PushTopic) records per org in Salesforce.
- For Performance and Unlimited Editions: 100
- For Enterprise Edition: 50
- For All other supported Editions: 40
- Maximum number of concurrent clients (subscribers) across all channels and for all event types
- For Performance and Unlimited Editions: 2000
- For Enterprise Edition: 1000
- For All other supported Editions: 20
- Maximum number of delivered event notifications within a 24-hour period, shared by all CometD clients
- For Performance and Unlimited Editions: 1,000,000
- For Enterprise Edition: 200,000
- For All other supported Editions: 50,000 (10,000 for free orgs)
- Maximum length for a PushTopic name is 25 Char
Push Topic Demo in Salesforce
Best Use Cases of Push Topic
It is a legacy event-driven architecture and is suited when you wish to publish changes, using Push Topics, based on certain Salesforce record changes. You define your own push criteria using a predefined SOQL query.
- PushTopics Use Case 1 – An external customer service application needs to invoke its own workflow of sending a thank you note based on an opportunity getting closed in Salesforce.
- PushTopics Use Case 2 – A Salesforce app needs to refresh certain fields based on the changes made in a Salesforce case record getting closed.