Is there any way to consistently execute a piece of Apex code whenever a User logs in to Salesforce? Posts such as this one seem to suggest that triggers on updates to the User object's 'LastLoginDate' field do not consistently execute.

Workflow would be okay too --- except that you cannot make workflow rules on the User object.

Is there any way to get a trigger to execute whenever a Salesforce user logs in?

Here is what I've currently done, both with the trigger saved in API v12 and API v25, with NO success. I have a Login_Events__c Long Textarea field that I'm trying to update based on some details of the User login event, but no such updates are taking place:

trigger User on User (before update) {

    if (Trigger.isBefore && Trigger.isUpdate) {

        for (User u : Trigger.new) {

            // Make sure there's at least something in the Login Events field
            if (u.login_events__c == null) u.login_events__c = '';

            // Add in the current login events change
            u.login_events__c +=
               '\n User Id: ' 
               + u.Id
               + ', Username: ' + u.Username 
               + ', Last Login Date: ' 
               + u.LastLoginDate 
               + ', Session Id: ' +  UserInfo.getSessionId() 
               + '\n';     


  • That thread was last updated almost 4 years ago; have you tried using a trigger? It may be more reliable now.
    – Mike Chale
    Commented Mar 22, 2013 at 17:42
  • I've tried using a trigger --- no luck. You would expect that LastLoginDate would be updated every time a user logs in. I'm not getting any trigger actions to execute. I'll post my code (that's not working).
    – zachelrath
    Commented Mar 22, 2013 at 17:58
  • 2
    Could you use a frequently running batch job which polls the LoginHistory table? Commented Mar 22, 2013 at 18:18
  • @techtrekker No, I need to do be able to execute this logic within the context of a single login event, so that I have the full context (e.g. UserInfo) of the transaction while it's being executed.
    – zachelrath
    Commented Mar 22, 2013 at 18:43

3 Answers 3


Couple of options to consider:

  1. Throw a Visualforce page into the sidebar component and either update the user record (or directly perform the logic you need to capture "login" event.
  2. Have a VF page and set that as the default page for the default App. Everytime the user logs in this page will get executed and then you can redirect to the tab/page that you want the user to go.

With #1, since Reports &Admin setup pages don't get the side bar, this might not work in those pages. So if a user had bookmarked a setup page, side bar component will never get executed.

With#2, if a user bookmarks a page, the default app/page is not used and hence the VF page will not be run.

What are you trying to achieve with this trigger? Given you are concatenating Session Id (which is temporary) not really sure what you are aiming to do.

Perhaps a use cae would help to provide better answers.

  • I think that just executing some code on the default page that users will see when they login to Salesforce is what I'll have to do.
    – zachelrath
    Commented Apr 13, 2013 at 18:09

Not sure if this thread is still active.You can use the login flows. Create a class with an invokable method and refer this class in flow by creating a new flow. Now create a login flow to invoke this newly created flow.

  • This is a better solution. I had similar requirements. Created a flow of type flow. Then created a login flow to execute the previously created flow when user logged in.
    – abdn
    Commented Apr 7, 2016 at 14:56
  • can you explain more about login flow? i know login flow only applicable for screen flow only
    – hendra
    Commented Nov 3, 2023 at 4:41

While not as good as the login flow solution, here is another alternative:

You could created a Scheduled job that runs every N minutes, and checks for users logged in within those last N minutes...

Datetime timeFilter = Datetime.now().addMinutes(-10);
List<User> users = [SELECT LastLoginDate, Name 
    FROM User 
    WHERE LastLoginDate != NULL 
    AND LastLoginDate > :timeFilter 
    ORDER BY LastLoginDate DESC 

You could then update your login history table from there.

You must log in to answer this question.

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