I was experimenting with Platform Events stumbled upon this issue. Can we access Id field of Platform Events in after insert trigger?

Below is my sample code:

trigger AlertTrigger on Alert__e (after insert) {
      for(Alert__e al: Trigger.new){

So when line number 3 executes it prints the Serialized JSON with record Id present


But on 4th Line when I try to access Id field I get

 System.SObjectException: Invalid field Id

This is a bit weird, as I planned on storing eventID in a custom record for audit purpose.


Platform Events are not normal objects; they lack access to many of the normal fields you'd expect to see. Here's a no-custom-field describe of a Platform Event:

Platform Event Describe

Notice the lack of Last Modified audit fields, etc. There's not even a Name field that almost all objects have. I suspect that since Id isn't directly exposed, it may not even be a unique value and/or generated in sequence, perhaps as an optimization allowing the database to support potentially billions of daily transactions.

As far as I can tell, there's also no guarantee that ReplayId will also be unique for all time for the event, so you probably can't depend on either being unique beyond the 24 hour time window. For now, if you absolutely insist on using the ID, you apparently already have your answer:

System.debug((Map<String, Object>)JSON.deserializeUntyped(al).get('Id'));

However, this is not guaranteed to work in future releases, since the Id is intentionally not exposed to developers. Depending on what you're trying to do, you might consider using Crypto.generateMac based on the ReplayId and CreatedDate to form a reasonably unique value that will have a very low rate of collision.

| improve this answer | |
  • @sfdcfox On "there's also no guarantee that ReplayId will also be unique for all time for the event", while researching for Pranay's question on this topic, looks like it is unique. However just not sure, what's the limit of the generated ids. As, if it reaches to say 999, will it reset to 1 or goes to 1000. – Jayant Das Apr 20 '18 at 1:00
  • @JayantDas It's simply not documented, so you must not make any assumptions about Id values being reused or not. The only reasonable guarantee that we have is that the same Id won't be used within the same 24 hour period (to allow replaying). Unless/until someone official at salesforce.com outright states that they are unique, we must not assume that to be true. For all we know, it restarts every month, or after 1,000,000, or something. We just simply can't know. – sfdcfox Apr 20 '18 at 2:09
  • @sfdcfox Completely agree. And that’s what exactly we don’t know if there’s a limit accounted for or not. In fact the replay ids should not be considered for any uniqueness outside of the event scope. – Jayant Das Apr 20 '18 at 2:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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