You need to sign in to do that
Don't have an account?
Becky 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.
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.
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.
All Answers
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.
@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?