What you need to know about Pushtopics in Salesforce

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

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:

  1. 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.
  2. 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.
  3. 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.
  4. 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

Unfortunately, Pushtopic queries do suffer from some limitations as not all SOQL operators are allowed for use in the WHERE clause of the query. For a full list of unsupported queries, have a look here.

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.

Get started!

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!

The following two tabs change content below.

Lukas Heinen

Lukas is a Junior Developer at Aptly. Since he's a qualified IT specialist, his blogposts broach the issue of everday as well as special challenges and experiences, which evolve from the usage of webbased cloud-technologies like Salesforce.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.