function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Derek DolanDerek Dolan 

User trigger not working as expected

I am looking to point to the last login time on the user object as part of a solution I am building. However neither the process builder nor formulas allow us to point to the standard out of the box user field called Last login time. I found a trigger on another thread that I could leverage

this is the trigger 
trigger lastLoginDate on User(before update){
User u = [SELECT LastLoginDate FROM User WHERE Id =:UserInfo.getUserId()];
for(User c : Trigger.new){        
    c.last_login__c= u.LastLoginDate;
    }
}

It works but I must hit the edit save button on the user record in order for the trigger to fire. What I need is for the trigger to fire when a user actaully logs in . I had assumed that a user logging in would fire the trigger by updating the standard last login field and therefore update my new custom last login field

Any ideas ?

I thought I could build a process to hit a refresh check box every day on all users, this may work as I only need the login day for the solution.

OR

Is there anyone who knows if I could treak the above trigger to fire when someone actaully logs in ?

 
Best Answer chosen by Derek Dolan
Khan AnasKhan Anas (Salesforce Developers) 
Hi Derek,

Greetings to you!

There's an idea which is active on the success community with a similar discussion for which you can upvote so that it gets available in the future.

https://success.salesforce.com/ideaView?id=08730000000BowqAAC

However, you can use the login flows:
https://developer.salesforce.com/docs/atlas.en-us.securityImplGuide.meta/securityImplGuide/security_login_flow_examples.htm

https://help.salesforce.com/articleView?id=security_login_flow.htm&type=5 (https://help.salesforce.com/articleView?id=security_login_flow.htm&type=5)

Please refer to the following link, it has a couple of suggestion on how you can capture user login and perform an action:

https://salesforce.stackexchange.com/questions/9926/execute-an-action-run-a-trigger-every-time-a-user-logs-in-to-salesforce

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Derek,

Greetings to you!

There's an idea which is active on the success community with a similar discussion for which you can upvote so that it gets available in the future.

https://success.salesforce.com/ideaView?id=08730000000BowqAAC

However, you can use the login flows:
https://developer.salesforce.com/docs/atlas.en-us.securityImplGuide.meta/securityImplGuide/security_login_flow_examples.htm

https://help.salesforce.com/articleView?id=security_login_flow.htm&type=5 (https://help.salesforce.com/articleView?id=security_login_flow.htm&type=5)

Please refer to the following link, it has a couple of suggestion on how you can capture user login and perform an action:

https://salesforce.stackexchange.com/questions/9926/execute-an-action-run-a-trigger-every-time-a-user-logs-in-to-salesforce

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Derek DolanDerek Dolan
Thanks for this detail

DD