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
Ashwin Kumar 80Ashwin Kumar 80 

10 Users loginhistory using Maps

Hi,

I want to get 10 users last 10 days loginhistory using map collection.
I am trying with below code, But I am not getting expected result . please help me.
 
Map<Id,User> users =new Map<Id,User>([Select Id from User Limit 10]) ;
system.debug('Users-->'+users);
Map<Id,List<LoginHistory >> loginDetals = new Map<Id,List<LoginHistory >>();
for(String uIds : users.keySet()){
    loginDetals.put(uIds , [SELECT Id, UserId,LoginTime from LoginHistory where UserId=:uIds and LoginTime=LAST_WEEK]);
	system.debug('put in map-->'+loginDetals);
    }
 
set<id> uIds= new set<id>();
for(User ac: [Select id from User limit 10]){
    uIds.add(ac.id);
}
system.debug('Users-->'+uIds);
Map<Id,List<LoginHistory >> loginDetals = new Map<Id,List<LoginHistory >>();
List<LoginHistory> LHList = [SELECT Id, UserId,LoginTime from LoginHistory where UserId=:uIds and LoginTime=LAST_WEEK Limit 50000 ];
system.debug('Login Details-->'+LHList);
for(LoginHistory lh:LHList)
{    If(!loginDetals.ContainsKey(lh.UserId))
    {
        loginDetals.put(lh.UserId,new List<LoginHistory>{lh});
        system.debug('put in map-->'+loginDetals);
    }
}

please help me.
Best Answer chosen by Ashwin Kumar 80
Priyananth RPriyananth R
Hi Ashwin,

Just try below code,
set<id> uIds= new set<id>();
for(User ac: [Select id from User limit 10]){
    uIds.add(ac.id);
}
Map<Id,List<LoginHistory >> loginDetals = new Map<Id,List<LoginHistory >>();
List<LoginHistory> LHList = [SELECT Id, UserId,LoginTime from LoginHistory where UserId=:uIds and LoginTime=LAST_N_DAYS:10 Limit 50000];
for(LoginHistory lh:LHList)
{   
    List<LoginHistory> lhList = new List<LoginHistory>();
    if(loginDetals.containsKey(lh.UserId)) {
        lhList = loginDetals.get(lh.UserId);
    } 
    lhList.add(lh);
    loginDetals.put(lh.UserId,lhList);
}
system.debug('Login Details-->'+loginDetals);

Thanks,

All Answers

Priyananth RPriyananth R
Hi Ashwin,

Just try below code,
set<id> uIds= new set<id>();
for(User ac: [Select id from User limit 10]){
    uIds.add(ac.id);
}
Map<Id,List<LoginHistory >> loginDetals = new Map<Id,List<LoginHistory >>();
List<LoginHistory> LHList = [SELECT Id, UserId,LoginTime from LoginHistory where UserId=:uIds and LoginTime=LAST_N_DAYS:10 Limit 50000];
for(LoginHistory lh:LHList)
{   
    List<LoginHistory> lhList = new List<LoginHistory>();
    if(loginDetals.containsKey(lh.UserId)) {
        lhList = loginDetals.get(lh.UserId);
    } 
    lhList.add(lh);
    loginDetals.put(lh.UserId,lhList);
}
system.debug('Login Details-->'+loginDetals);

Thanks,
This was selected as the best answer
Raj VakatiRaj Vakati
Use this
 
set<id> uIds= new set<id>();
for(User ac: [Select id from User limit 10]){
    uIds.add(ac.id);
}
Map<Id,List<LoginHistory >> loginDetals = new Map<Id,List<LoginHistory >>();
List<LoginHistory> LHList = [SELECT Id, UserId,LoginTime from LoginHistory where UserId=:uIds and LoginTime=LAST_N_DAYS:10 Limit 50000];
for(LoginHistory lh:LHList)
{ 
if(!loginDetals.containsKey(so.ParentId))
        loginDetals.put(lh.UserId, new List<LoginHistory>{lh});
}else{
	loginDetals.get(lh.UserId).add(lh);
}
}
system.debug('Login Details-->'+loginDetals);

 
Ashwin Kumar 80Ashwin Kumar 80
Hi  Priyananth R,

It working as expected, Thanks for the help :) .

Regards,
Ashwin