0

I would like to apologise if any spelling mistakes , in currently posting this on an iPhone whilst I commute home

I found a link online which I think meets my requirements

As we are all aware lastlogin date on user object is accessible via formulas and triggers. However I been told it can be accessed via visual force pages and a batch apex. I don't want to go down the route of visual force so in looking at batch apex.

I want to get the last login date from the user object an populate anther field in a custom object. The field I would like to populate is abc_test, and object is called mason

I have found the following trigger but I don't no why field names I need to change to meet my critters, and whether this trigger should be put in my custom object or user object

I am aware that this needs to be scheduled job aswell

Looking forward to your help D

public void ResetlastLogin_Update(List<User> oldUsers, List<User> newUsers) {

System.debug('ResetlastLogin_Update: entering trigger');

List<Id> idsToUpdate = new List<Id>();

for (integer i=0; i<newUsers.size(); i++) {
 User newVals = newUsers[i];
  User oldVals = oldUsers[i];

 if (newVals.lastlogindate != oldVals.lastlogindate__c) {
      idsToUpdate.add(newVals.Id);
 }
}
System.debug('Ids to Update: ' + idsToUpdate.size());

if (idsToUpdate.size() > 0) {

List<User> usersToUpdate = [SELECT Id, lastlogindate, lastlogindate__c FROM User WHERE Id  IN :idsToUpdate];
for (User u : usersToUpdate) {

 if (u.lastlogindate__c == NULL ) {

 u.lastlogindate__c = u.lastlogindate ;
 }
}
update usersToUpdate;

}

System.debug('ResetlastLogin_Update_Update: exiting trigger');
}
4
  • Are you having any issues with this? Does it work?
    – EricSSH
    Jul 15, 2014 at 17:54
  • Ericssh - I haven't tested it, but I would assume I would need to change field names to reference the field I want to populate and in what object. However I am unsure what fields I would need to change.
    – Masond3
    Jul 15, 2014 at 17:58
  • You're going to have to put in your custom object, and mess around with it, but it appears this would be a good start
    – EricSSH
    Jul 15, 2014 at 18:09
  • Where would I reference my custom object though ? If I'm reading the above correctly the trigger is extracting the logins I'd and date , and then populating another field with that date , but what forks is he populating ?
    – Masond3
    Jul 15, 2014 at 18:14

1 Answer 1

1

You have all the data you need, after you get the data out of the User object you will need to push it over to your custom object..

for(customObject c :[Select something FROM customObject WHERE CreatedById(maybe?) In :idsToUpdate]){
c.somethingToUpdate = usersToUpdate.get(c.somethingToUpdate).LastLoginDate;}

This is super rough, and should be taken with a grain of salt so don't copy and paste this and expect it to work. Try something and come back with some more info

You must log in to answer this question.

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