In Salesforce context, a Pushtopic is a feature that is a part of the Salesforce Streaming API and enables users to recieve a notification if Salesforce records change and meet a predefined cirteria. Here are its benefits and how to use a Pushtopic.
In Salesforce context, a Pushtopic is a feature that is a part of the Salesforce Streaming API. It „represents a query that is the basis for notifying listeners of changes to records in a Salesforce organization“ (Source). Please note that Pushtopics are only available in Salesforce if the Streaming API is enabled.
Generally speaking, a Pushtopic is supposed to enable listeners within a Salesforce instance as well as application servers and clients outside of Salesforce to receive a notification if one or multiple Salesforce records meet the predefined criteria of a Pushtopic.
The Push technology, also known as the „publish/subscribe model“, is a model of internet-based communication. In our case, the server has a pre-defined set of data (defined via a SOQL query) and a list of subscribed clients. Once a piece of data meets the specified criteria, a notification is sent to the clients, containing at least the IDs of the record that met the criteria.
What are the benefits of using a Pushtopic?
Pushtopics enable Salesforce users to work in real time. A concrete use case was provided by one of our customers: they had the need to process incoming Leads and Opportunities in real time. By the use of Pushtopics qualifying Leads and Opportunities are pushed to a visualforce page in real time. Sales users with access to this page are able to follow up on Leads and Opportunities as soon as those met the defined criteria, without experiencing any delay.
Be it the fast identification of key customers, implementing a high priority target system, or whatever case you can imagine that involves the processing of data in real time – Pushtopics are one way to go for that.
How to use a Pushtopic?
Before creating a Pushtopic, it is necessary to fully understand all configurative options that a Pushtopic offers.
Salesforce offers two ways for the creating process. One is through the Developer Workbench, the other one is through apex code that can be executed anonymously via the Developer Console.
Example: insertion via Code
Pushtopic myNewTopic = new Pushtopic
ApiVersion = 36.0, // or any API-Version you desire
Description = 'This Pushtopic listens to Opportunities.',
isActive = true, // default
Name = 'Test_Opportunity'
NotifyForFields = 'Referenced', // default
NotifyForOperationCreate = true,
Query = 'SELECT Id, Amount FROM Opportunity'
A Pushtopic can be treated like any other Salesforce object and may be subject to DML. It also supports SOAP and REST calls. For more details, please check out the documentation available in the developer guide.
How is the the Pushtopic evaluated?
The Pushtopic evaluation is based on the query that is specified as an attribute of the Pushtopic itself. If the query contains the field „Amount“, then a change to that field would trigger the evaluation rules of the Pushtopic. The Pushtopic will evaluate any record meeting the criteria specified in the SOQL query and send a notification to the clients if an event that is supposed to create a notification is being triggered.
The default settings for a Pushtopic are:
- Events that trigger a notification: all (create, update, delete, undelete)
- Fields that are evaluated for an event: all fields referenced in the SOQL query
The query from the example above („SELECT Id, Amount FROM Opportunity“) would send a notification in every case of a change to the „Amount“ field. Also, only the fields that are within the query will be pushed to the client (in this case, „Id“ and „Amount“).
One more thing:
The NotifyForFields attribute of the Pushtopic is responsible for the evaluation of the fields. The following settings are possible:
- All: Notifications are generated for all record field changes, provided the values of the fields referenced in the WHERE clause match the values specified in the WHERE clause.
- Referenced (default): Changes to fields referenced in both the SELECT clause and WHERE clause are evaluated. Notifications are generated for all records where a field referenced in the SELECT clause changes or a field referenced in the WHERE clause changes and the values of the fields referenced in the WHERE clause match the values specified in the WHERE clause.
- Select: Changes to fields referenced in the SELECT clause are evaluated. Notifications are generated for all records where a field referenced in the SELECT clause changes and the values of the fields referenced in the WHERE clause match the values specified in the WHERE clause.
- Where: Changes to fields referenced in the WHERE clause are evaluated. Notifications are generated for all records where a field referenced in the WHERE clause changes and the values of the fields referenced in the WHERE clause match the values specified in the WHERE clause.
This explanation was taken from the official documentation.
Limitations and how not to be stopped by them
However in almost every case it is possible to create a utility field on the object that the Pushtopic is supposed to evaluate and fill that utility field by a trigger. The pushtopic then only needs to listen to this utility field. While this might always not be a go-to solution and depends on the use case, it is a legitimate workaround to deal with the query limitations.
The last step is now to try it out for yourself. Go ahead and check the Streaming API documentation (up to date to Spring/2016 as of today). It contains various step-by-step examples about how to create, maintain and subscribe to a Pushtopic, including an external Java client example.
I for one hope I could bring the idea of Pushtopics one step closer to you. If you have any questions feel free to leave them in the comments. Thanks, and bye!
Neueste Artikel von Lukas Heinen (alle ansehen)
- What you need to know about Pushtopics in Salesforce - 12. Mai 2016
- Agile Alternatives: Kanban - 1. Februar 2016
- Salesforce Deployment – Best Practices - 10. November 2015
- The Salesforce Tooling API - 28. Juli 2015
- Breaking up – a short workaround on Salesforce tables to remain scrollable - 19. Dezember 2014