Salesforce Streaming API: Create Push Topics

Looking for Salesforce Training & HandsOn Projects?

Trailblazer Profile | LinkedIn | Salesforce Blog | Facebook | Youtube Channel | WhatsApp Community

Push Topic is a publish-subscribe mechanism within the Salesforce Streaming API to receive real-time notifications about changes to data in your org. You define a specific set of criteria through a SOQL query, and whenever matching records are created, updated, deleted, or undeleted, Salesforce pushes notifications to subscribed clients instantly.

Push Topic – Execution Workflow

The client subscribes to a PushTopic with its identifying name (/topic/<PushTopic_Name>).

Salesforce evaluates record changes against all active PushTopics.

If a change matches a PushTopic's criteria, a notification is generated containing record details and sent to subscribed clients.

The client receives the notification and can take appropriate actions based on the data change.

While Salesforce Push Topics are a legacy feature, there are still some benefits to using them in specific situations. Here are some key advantages:

  • Ease of use: Push Topics offer a relatively simple way to set up real-time notifications compared to newer features like Platform Events or Change Streams. Their SOQL-based query definition makes them readily understandable for those familiar with Salesforce queries.
  • Reduced server load: Unlike traditional polling approaches, Push Topics only sends notifications to interested clients, minimizing server load and bandwidth usage. This can be beneficial for situations where data changes aren't frequent or only a subset of users needs to be notified.
  • Real-time awareness: Push Topics offer near real-time notifications, which can be crucial for scenarios where immediate action is necessary upon data changes. This can improve team reactivity and overall efficiency in responding to critical events.
  • Customization: While not as robust as Platform Events, Push Topics still allow for some customization through the SOQL query, enabling users to define the precise data they want to track and receive notifications for.
  • Historical compatibility: If your existing platform relies on Push Topics, switching to newer features might require significant rework. Utilizing Push Topics can maintain current functionality while evaluating and preparing for a future migration to more advanced options.

Remember, these benefits exist within the context of Push Topics being a legacy feature. Here are some limitations to consider:

  • Technical constraints: Limited record size, number of SOQL filters, and polling frequency can hinder complex scenarios or large data volumes.
  • Limited functionality: Push Topics lack features like durable delivery, complex event processing, and integration with other streaming services offered by newer alternatives.
  • Reduced future support: As a legacy feature, Push Topics are unlikely to receive future enhancements or extensive support from Salesforce.
  • Potential migration challenges: Moving away from Push Topics in the future might involve significant development efforts if your platform heavily relies on them.

Demo: Create a Push Topic

1. Access the Developer Console:

  • Click on the gear icon in the top right corner and select Setup.
  • Search for and open Developer Console.

2. Define the PushTopic

  • In the Developer Console, click on Debug and then open the Execute Anonymous Window.
  • Copy and paste the following code, replacing "your_SOQL_query" with your specific query that identifies the record type and fields you want to track.
  • Click Execute.




Here is a full explanation of each of the properties used to create a Push Topic Definition:

1. Required Properties:

  • Name: A unique and descriptive name for your PushTopic (max 25 characters).
  • ApiVersion: API version to use for executing the SOQL query. Must be greater than 20.0 and match the package's version for custom objects.
  • Query: Your SOQL query defines the record type and fields you want to track for notifications.

2. Optional Properties:

  • Description: Optional description of your PushTopic (max 400 characters).
  • IsActive: Determines if the PushTopic counts towards your org's allocation (true) or not (false).
  • NotifyForFields: Specifies which fields trigger notifications on changes. Options include:

    • All: Notify for all field changes.
    • Referenced (default): Notify for changes to referenced fields (lookups, relationships).
    • Select: Notify for changes only to specified fields (using SELECT in your SOQL query).
    • Where: Notify based on the conditions specified in the WHERE clause of your SOQL query.
    • NotifyForOperationCreate
      • When NotifyForOperationCreate is set to true (which is the default), your reporter will include notifications for new records being created that match your PushTopic's query.
      • If you set NotifyForOperationCreate to false, your reporter will only send notifications for updates or deletions to existing records, but not for new ones.
    • NotifyForOperationDelete
      • If NotifyForOperationDelete is set to TRUE:
        • You'll receive notifications whenever a record matching your PushTopic's query is deleted from Salesforce.
        • Think of it as getting a breaking news alert when something important disappears.
      • If NotifyForOperationDelete is set to FALSE:
        • You won't receive notifications for deletions.
    • NotifyForOperationUndelete
      • When it's set to "true," your PushTopic will send you a notification whenever a record that matches your query is undeleted. This means someone has rescued it from the recycle bin and put it back into active use.
      • When it's set to "false," your PushTopic will ignore undeletion events. It'll focus on other changes like new records being created, existing records being updated, or records being permanently deleted.
    • NotifyForOperationUpdate
      • Value: This property is a Boolean, meaning it can only be true or false.
      • Default: By default, it's true, which means you will receive notifications for record creations along with other update operations (like edits or deletions).
      • Setting to false: If you only care about updates made to existing records and don't need to be notified about newly created ones, you can set NotifyForOperationUpdate to false. This can be helpful if you're already receiving many notifications and want to reduce the noise.

Upon successful execution of the PushTopic creation process, the verification of its existence within the system can be performed by executing a SOQL query against the PushTopic object. This query will retrieve and display the newly created record, confirming its successful instantiation.

Conclusion

For most real-time data needs, utilizing newer and more powerful options like Platform Events and Change Streams is strongly recommended. While Push Topics might still work for simple scenarios, their limitations and lack of future development make them a declining choice in the evolving world of Salesforce real-time data solutions.

Trailblazer Profile | LinkedIn | Salesforce Blog | Facebook | Youtube Channel | WhatsApp Community

Comments