2

I just want to know if you can query a platform event?

1
  • 2
    This question is really short. I won't downvote it, but I do think that this question would be better if you included some information about research that you've already done on the topic (with links, as appropriate).
    – Derek F
    Jul 3, 2018 at 13:04

2 Answers 2

21

Other answers have shown that it's not supported to query events using SOQL, but I want to clarify that it's not only not supported, it is not meaningful.

Platform events form an ordered time stream. Each event has a specific "replay ID", and each event's replay ID is higher (not necessarily 1 higher, but higher) than the previous event's. They're guaranteed to be delivered in a specific order to clients that are watching the event stream, such as Platform Event triggers and external subscribers.

There is a replay mechanism whereby clients that "missed" part of the stream can supply the last replay ID they received and have all of the event between then and the current moment resent to them for processing (again, in order). However, this replay window only covers 24 hours.

Events are intended to be ephemeral ordered messages, not random-access data storage. An event would be something like "Object X changed!", where the underlying data store is Object X. You can query that object, but it wouldn't make sense to query the event itself. An individual event may or may not contain enough information to carry meaning outside its position in the timestream and the state of the object at that time.

If you need change history tracking that's queryable, you should use a different feature that's made to do that.

3
  • Every streaming platform out there provides a way to look into the messages they store. Why should SF not provide that mechanism, specially if I want to replay? Say external application says it has published but SF missed consuming them due to issues with the Trigger Handler. I should be able to look at the messages and replay them from where they errored out without having to ask the External party to re-publish. Is that not the whole point of maintaining them in a 'Bus'?
    – JaganY
    Jan 10, 2021 at 18:01
  • I don't understand your question. You may wish to open a new post.
    – David Reed
    Jan 10, 2021 at 18:06
  • Sorry, I was merely commenting on your comment. You were justifying how Platform Events (PEs) are not meant to be queried but what I think SF should provide a way to view (query) and 'replay' them from within like in the case when the PE trigger handler failed(say with an exception), code fixed and now I want to re-consume the events in the Comet-D bus.
    – JaganY
    Jan 12, 2021 at 0:26
3

From Salesforce documentation:

https://developer.salesforce.com/docs/atlas.en-us.platform_events.meta/platform_events/platform_events_considerations.htm

No SOQL Support You can’t query event notifications using SOQL.

Not the answer you're looking for? Browse other questions tagged or ask your own question.