
Hi Everyone, Welcome again ;). In this tutorial, we are going to learn how we can capture the Logout Event. We can perform any operation after user Logout like we can create the LogOut event records to track how many hours a user is working or logging hours.
To capture the Logout Event, you need to Enable “Enable Logout Events Stream” setting under “Session Setting”.
Enable Logout Events Stream is a beta feature and to make this available for your org you need to contact Salesforce Support. Once they will enable the setting then you can enable the Setting and Develop the Trigger on the LogOutEventStream Object.
Once You have contacted your salesforce support and then have enabled the option. Follow the below steps.
Go to Setup -> Session Setting –> Scroll Down till “Logout Events” section and Check “Enable Logout Events Stream” Checkbox.

Create a New Custom Object to Store the Logout Event

Now, as you have created the Custom object to Store the Events create a Trigger and use below code for the trigger
trigger LogoutEventTrigger on LogoutEventStream (after insert) {
List<LogoutEvent__c> eventList = new List<LogoutEvent__c>();
For(LogoutEventStream event : Trigger.new){
LogoutEvent__c record = new LogoutEvent__c();
record.EventIdentifier__c = event.EventIdentifier;
record.UserId__c = event.UserId;
record.Username__c = event.Username;
record.EventDate__c = event.EventDate;
record.RelatedEventIdentifier__c = event.RelatedEventIdentifier;
record.ReplayId__c = event.ReplayId;
record.SessionLevel__c = event.SessionLevel;
record.SourceIp__c = event.SourceIp;
record.SessionKey__c = event.SessionKey;
record.LoginKey__c = event.LoginKey;
eventList.add(record);
}
insert eventList;
}
Now, go ahead and Log out. Login again and go to the object and you will be able to see the logout event record.

Sharing is caring 🙂 😉 Keep learning. Happy Trailblazing.
Resources: –
- https://help.salesforce.com/articleView?id=security_auth_create_logout_event_trigger.htm&type=5
- https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_logouteventstream.htm
- https://dreamevent.secure.force.com/articleView?id=security_auth_create_logout_event_trigger.htm&type=0
7 comments
I am already a big fan of SfDc panther .first your lightning videos now this series .thanks a lot Amit
Thank You Shyam 🙂
Great post,in the end it should be eventlist.add(record)
Hi Kunal,
Thanks for the catch I have made that change. Thanks Again 🙂
Thanks for sharing 🙂
This Feature is good, but how to write UnitTest, the LogoutEventStream object can’t be inserted on Test class, it throws an error: “FATAL_ERROR|System.DmlException: Argument must be of internal SObject type. use insertAsync() or insertImmediate() instead”
Any idea?
Hey Munoz,
Unfortunately, I am also stuck on the same :(. I will update you once I will find the answer 🙂
Thanks & Regards,
SFDCPanther