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.
Enable Logout Events Events
Go to Setup -> Session Setting –> Scroll Down till the “Logout Events” section and Check “Enable Logout Events Stream” Checkbox.
Create a New Custom Object to Store the Logout Event
Develop Apex Trigger
Now, as you have created the Custom object to Store the Events create a Trigger and use the 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;
}
Develop the test class
Use the below code for test class
https://gist.github.com/amitastreait/427cc6d066f7e77ffd7b9b8b40263a84
Test the Trigger
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
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
Will it work, when user closes the browser and do not click on logout button. Our login is SSO enabled.
Unfortunately, it will only work when the user logs out from the button.
Hi there. Just come across this. Did you ever find a solution to the test class issue – as I also have the same problem as mentioned above: “System.DmlException: Argument must be of internal sObject type. use insertAsync() or insertImmediate() instead” ?
This is a platform event try publishing the event from your test class.
LogoutEventStream stream = new LogoutEventStream(
);
EventBus.publish(stream);