Salesforce User Logout Event Trigger
- Salesforce has introduced LogoutEventStream in Summer’18 release, to get the User logout events.
- It is a beta feature and to make it available in your org, you need to contact Salesforce Support.
- After it will available in your Org, you can enable LogoutEventStream.
- After enabled LogoutEventStream, Salesforce publishes logout events when users log out from the UI.
- You can add an Apex trigger to subscribe to those events, where you can implement our custom logic during logout.
Enable Logout Events Stream :
Go to Setup | Event Manager (Enter Event Manager in Quick Find) | Click on Logout Event link and click on Update Event on right coroner button and select Enable Streaming.
After enable Logout Events Stream, I have created an Apex trigger on LogoutEventStream object, where I’m saving the User Logout Event information in a Custom Object.
Apex Trigger:
trigger LogoutEventTrigger on LogoutEventStream (after insert) { List<LogoutEventInfo__c> leInfoList = new List<LogoutEventInfo__c>(); for(LogoutEventStream les : Trigger.new){ LogoutEventInfo__c leInfo = new LogoutEventInfo__c(); leInfo.EventIdentifier__c = les.EventIdentifier; leInfo.UserId__c = les.UserId; leInfo.Username__c = les.Username; leInfo.EventDate__c = les.EventDate; leInfo.RelatedEventIdentifier__c = les.RelatedEventIdentifier; leInfo.SessionKey__c = les.SessionKey; leInfo.LoginKey__c = les.LoginKey; leInfo.ReplayId__c = les.ReplayId; leInfo.SessionLevel__c = les.SessionLevel; leInfo.SourceIp__c = les.SourceIp; leInfoList.add(leInfo); } Insert leInfoList; }
Now, you can Logout and Login again to check the Logout Event information in Custom Object.