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';
}
}
}