0

I'm trying to execute some Apex in a LogoutEventStream trigger that checks against the user's sessionId (SessionManagement.getCurrentSession().get('SessionId'), however the SessionKey field provided by the LogoutEventStream does not match. Is there an encoding that is being used to transform it? Is there another way to match with the user's sessionId during a logoutevent?

Note, I unsuccessfuly attempted to use getCurrentSession in the Apex --> "System.UnexpectedException: Current session unavailable". I assume it failed because in the context of a LogoutEventTrigger, the session no longer exists. I also attempted to fire the trigger before insert but this is not supported for platform events.

2
  • 1
    The Platform Event runs as a separate transaction and as Automated Process user so it makes sense there is no match between value in the PE and the current trigger user's session
    – cropredy
    Dec 9, 2022 at 22:41
  • @cropredy I'm using the logouteventstream to define the event of "a session ended". Then, I want to know which session ended, so I can match it up to the login flow that I executed previously. What I was hoping for is the following: (Event published) "LogoutEventStream published" --> (Event handled) "A session ended" --> (My code) "Which session ended?" --> Get Session from MySessions where SessionId = thisLogoutEvent.SessionKey So, aside from the logouteventstream, is there a way to check an ended session against existing sessions? Dec 12, 2022 at 13:48

1 Answer 1

1

The Platform Event runs as a separate transaction and as Automated Process user so it makes sense there is no match between value in the PE and the current trigger user's session

So, this is how I would approach it if you want to correlate logins to logouts:

You need to query two big objects

SELECT LoginKey FROM LogoutEvent

and

SELECT LoginKey FROM LoginEvent

From the doc

LoginKey

Type: string

Properties: Nillable

Description: The string that ties together all events in a given user’s login session. The session starts with a login event and ends with either a logout event or the user session expiring. This field is available in API version 46.0 and later. For example, lUqjLPQTWRdvRG4.

The SessionKey field is null on the LoginEvent object (which is auto-populated by the LoginEventStream event. The LogoutEvent big object is auto-created by the LogoutEventStream event. Note that user session expiry does not create a LogoutEventStream unless Force logout on session timeout is enabled (hat tip to @identigral for this)

1
  • 2
    Session timeout should trigger a logout event if Force logout on session timeout is enabled. As of this writing, there's a known issue with this feature.
    – identigral
    Dec 12, 2022 at 18:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .