TransWikia.com

Unable to get Last Login Date/Time of user on Contact Object

Salesforce Asked on November 17, 2021

I am trying to populate the User’s Last Login time on contact so I had created a Date/Time field Last_Login_Time__c. I had read multiple articles that it is not possible through formula field/Process Builder/Workflow (If there is any please let me know), so I am writing this trigger

trigger lastLogin on Contact (before update) {
User u = [SELECT LastLoginDate FROM User WHERE Id =:UserInfo.getUserId()];
for(Contact c : Trigger.new){        
    c.Last_Login_Time__c = u.LastLoginDate;
    }
}

In my case a User(Contact converted into User) logs into Portal and that generates the last login time on User.

When I am updating a contact record, I am getting my last login time rather than that user’s last login. I am sure I have to add some conditions in for Loop, can someone help me with this, thanks.

— Modified Code——-
Created a new field on User: Login_Time__c()

Trigger:

trigger loginUpdate on User(before insert, before update, after insert, after update){
    if(Trigger.isBefore){
        if(Trigger.isUpdate){
               loginUpdate_Handler.beforeUpdate(Trigger.New, Trigger.OldMap);
        }
    }
}

Handler Class:

public class loginUpdate_Handler{
    public static void beforeUpdate(List<User> newList, Map<id, User> oldMap){    
        Map<Id, User> id = new Map<Id, User>();        
        List<User> loginTimeList = new List<User>();

        for(User u: newList){
            if(u.user_email_Id__c != ''){
                loginTimeList.add(u);  
            }
        }        
        if(loginTimeList <> null)
           loginTime(loginTimeList);
    }    
    private static void loginTime(List<User> userList){
        for(User u : userList){
            LoginHistory lh = [SELECT Id,UserId,Logintime FROM LoginHistory WHERE UserId=:userinfo.getUserId()];
            u.Login_Time__c = lh.LoginTime;
        }
    }
}

I receive this error
Apex trigger loginUpdate caused an unexpected exception, contact your administrator: loginUpdate: execution of BeforeUpdate caused by: System.QueryException: List has more than 1 row for assignment to SObject: ()

I think I am complicating this code, can anyone help me with this, thanks

One Answer

Lets dissect your code:

Trigger lastLogin on Contact

This means your code runs whenever a Contact is updated, not when a User is updated. Judging from your description above, you may want this code to run on User update, not Contact update.

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

Here you are querying for the current user who has updated the Contact. This means that if you update the Contact, then you will be pulling your own information, not the desired user's information.

for(Contact c : Trigger.new){        
    c.Last_Login_Time__c = u.LastLoginDate;
}

Since you now have your Last Login Date, you are updating "c.Last_Login_Time__c" with your Last Login Date.

If you want the values to update whenever you personally update a Contact, then you need to query for the user by using the UserId field on the Contact.

If you want the value to update automatically on user login, you will need to make this trigger run on the User object, and query for the associated Contact.

Answered by Evan on November 17, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP