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
Becky MillerBecky Miller 

Updating the lastlogindate to a custom field

I am trying to populate user.lastlogindate into the custom field in the User Object called date_last_login__c.  I am getting an error + loop did not match anything at input 'trigger'


trigger lastloginDate on User(before update) {
User u = [SELECT LastLoginDate FROM User WHERE Id =:UserInfo.getUserId()];
for(User b : Trigger.new){
u.date_last_login__c = b.LastLoginDate;
}
}

On a sidenote I am very very new at coding.  So, if you have a different way I should be doing this please change it up.  Thank you so much.
Best Answer chosen by Becky Miller
Andy BoettcherAndy Boettcher
Becky,

The two other answers are close, but you want to make sure that you bulkify your code - to prevent any governor limit errors when you are updating more than 100 rows.

NOTE!  If you do a dataloader load on the User object, this code will fire and may throw off the metric you're trying to find.
 
trigger lastloginDate on User(before update) {

	// To handle bulk updates, get all combinations of User/LastLoginDate for all users in trigge scope
	Map<Id, User> mapUserDates = new Map<Id, User>([SELECT Id, LastLoginDate FROM User WHERE Id IN :trigger.newMap.keySet()]);

	// Loop through trigger.new
	for(User usr : Trigger.new){

		// Pull the LastLoginDate field from the User map
		usr.date_last_login__c = mapUserDates.get(usr.Id).LastLoginDate;
	}
}

 

All Answers

Andy BoettcherAndy Boettcher
Becky,

The two other answers are close, but you want to make sure that you bulkify your code - to prevent any governor limit errors when you are updating more than 100 rows.

NOTE!  If you do a dataloader load on the User object, this code will fire and may throw off the metric you're trying to find.
 
trigger lastloginDate on User(before update) {

	// To handle bulk updates, get all combinations of User/LastLoginDate for all users in trigge scope
	Map<Id, User> mapUserDates = new Map<Id, User>([SELECT Id, LastLoginDate FROM User WHERE Id IN :trigger.newMap.keySet()]);

	// Loop through trigger.new
	for(User usr : Trigger.new){

		// Pull the LastLoginDate field from the User map
		usr.date_last_login__c = mapUserDates.get(usr.Id).LastLoginDate;
	}
}

 
This was selected as the best answer
Becky MillerBecky Miller
Thank you all.  Now onto writing the Test process.   One question on that to do the Test process I will write a test class to execute the trigger.  Is that correct?
Andy BoettcherAndy Boettcher
Yup!  Your test class will first create a user, and then update it.  Be sure to use your System.Assert() functions to verify the outputs of your test!  =)
Becky MillerBecky Miller
I am not sure if this is right for what I was doing.  (but, heck I am trying).  Now, I did it this way no System.Assert() and it passed. 

@isTest
private class TestLastLogin {
    public static testMethod void TestLastLogin() {
        // Setup test data
        // This code runs as the system user
        Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 
        User u = new User(Alias = 'standt', Email='standarduser@testorg.com', 
            EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = p.Id, 
            TimeZoneSidKey='America/Los_Angeles', UserName='standarduser1@bmmm.com');

        System.runAs(u) {
            // The following code runs as user 'u' 
            System.debug('Current User: ' + UserInfo.getUserName());
            System.debug('Current Profile: ' + UserInfo.getProfileId()); 
        }
    }

So is this wrong?