How to Capture Salesforce User Logout Activity – LogoutEventStream

on

|

views

and

comments

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: –

Amit Singh
Amit Singhhttps://www.pantherschools.com/
Amit Singh aka @sfdcpanther/pantherschools, a Salesforce Technical Architect, Consultant with over 8+ years of experience in Salesforce technology. 21x Certified. Blogger, Speaker, and Instructor. DevSecOps Champion
Share this

Leave a review

Excellent

SUBSCRIBE-US

Book a 1:1 Call

Must-read

How to Utilize Salesforce CLI sf (v2)

The Salesforce CLI is not just a tool; it’s the cornerstone of development on the Salesforce Platform. It’s your go-to for building, testing, deploying, and more. As one of the most important development tools in our ecosystem

Save the day of a Developer with Apex Log Analyzer

Table of Contents What is Apex Log Analyzer? Apex Log Analyzer, a tool designed with Salesforce developers in mind, is here to simplify and accelerate your...

Salesforce PodCast

Introduction Hey Everyone, Welcome to my podcast, the first-ever podcast in India for Salesforce professionals. Achievement We are happy to announce that we have been selected as Top...

Recent articles

More like this

11 COMMENTS

  1. 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?

  2. 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” ?

LEAVE A REPLY

Please enter your comment!
Please enter your name here

5/5

Stuck in coding limbo?

Our courses unlock your tech potential