2

Aim – Pull the last login date from the user object into my custom object and populate a particular field

Now I am under the impression that LastLoginDate on the user object is not accessible through formulas in custom objects. The only way that LastLoginDate is accessible is through triggers

**Custom Object** Object Name Mason_Object
API Name Mason_Object__c

**Field I would like to update is on the mason_object** Field Label : LastLogin Api Name : LastLogin__c Data type: Date/Time

I have written the following trigger (see below) however its not pulling through any values. Any ideas why ?

trigger lastloginDate on Mason_Object__c(before update) {

User u = [SELECT LastLoginDate FROM User WHERE Id =:UserInfo.getUserId()];
trigger.new[0].LastLogin__c = u.LastLoginDate;

}

marked as duplicate by Ralph Callaway, Gaurav Kheterpal, Vamsi Krishna, Samuel De Rycke, highfive Sep 9 '14 at 7:20

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 3
    First thing that catches the eye is that your trigger is not very bulk-proof. However, that's not where your question is about..:-) What do you see in the debug logs if you put a system.debug(u); after the User query? – Guy Jul 10 '14 at 14:35
  • Have you tried after update? A lot of system fields aren't set until the after transaction, seems strange, but might be worth looking into to – Ralph Callaway Jul 11 '14 at 0:16
0
trigger lastloginDate on Mason_Object__c(before update) {
User u = [SELECT LastLoginDate FROM User WHERE Id =:UserInfo.getUserId()];
for(Mason_Object__c c : Trigger.new){
//May want to put some condition checks
c.LastLogin__c = u.LastLoginDate;
}
}

But can't this be achieved by some cross object formulas, or are you not able to manipulate an active user? I don't know I've never done cross object formulas on Users

Update to your comments --

The only way I can think to do it then is so whenever User.oldMap != User.newMap(pseudo), make that kick off your Custom Object and Update it

  • Hi Ericssh , unfortunately you are unable to pull back last login date from a formula. So only way to do is it via triggers . I logged into my dev account and replicated your logic how ever the trigger only fires once I need it to fire every-time a user logs in. Fr instance I logged in at 8 and it posted the login date, I signed out of sf and logged back in at 8.20 and the last login date in the object says 8 still.any ideas why ? – Masond3 Jul 10 '14 at 19:22
  • It's because the trigger isn't getting fired again, because it is "before update".. Is there ever any editing done to the page? Can you give me a Use case? – EricSSH Jul 10 '14 at 20:05
  • Hi, well I all want to do is pull the value in last login date on user object into my new custom object and field. The new object can edited after , but that shouldn't have any bearing on the value being populated . Currently when a user logins in it populates last login date with time stamp. The user can log into sf multiple times and the last login field reflects that's – Masond3 Jul 10 '14 at 20:13
  • Looks good in theory, although to my knowledge a user logging in doesn't cause the user triggers to fire, see the duplicates listed in the question comments. – Ralph Callaway Sep 9 '14 at 4:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.