whats the total number of ms in:
q: how to get unique user count logged into salesforce with specific permission set i am trying to get count of users logged in to salesforce in specific time who is having specific permission set. i am getting row more than 500001 error..
can we apply any filter in loginhistory to reduce the rows or any other way to get the unique user count logged in to salesforce in specific time.
datetime dt = system.now();
datetime earlier = dt.addminutes(-30);
datetime fullhour = dt.addminutes( -dt.minute() ).addseconds( -dt.second() );
set<id> userid = new set<id>();
set<id> usd = new set<id>();
list<permissionsetassignment> userlist = [ select assigneeid from permissionsetassignment where 
 permissionsetid in ( select id from permissionset where name = test_permission) limit 50000 ];
 for(permissionsetassignment us:userlist){
 usd.add(us.assigneeid);
   }
 list<loginhistory> loginhistorylist = [select id, userid from loginhistory where  logintime 
 >:earlier 
and userid in:usd ];
for(loginhistory lh:loginhistorylist){
userid.add(lh.userid);
}
 system.debug(user logged in count who is having test ps assigned-->+userid.size());


a: this code is untested, and is mostly yours with slight modifications. ive made modifications:
//we will later limit a query by numactiveusers to avoid your 50001 error.
lstactiveusers = [select count(id)totalactiveusers from user where isactive=true]; 
integer numactiveusers =(integer)lstactiveusers[0].get(totalactiveusers);
system.debug( numactiveusers:  + numactiveusers );

//your code, with some modifications to the query
datetime dt = system.now();
datetime earlier = dt.addminutes(-30);
datetime fullhour = dt.addminutes( -dt.minute() ).addseconds( -dt.second() );
set<id> userid = new set<id>();
set<id> usd = new set<id>();
list<permissionsetassignment> userlist = [ select assigneeid from permissionsetassignment where 
permissionsetid in ( select id from permissionset where name = test_permission) limit 50000 ];
for(permissionsetassignment us:userlist){
usd.add(us.assigneeid);
}
list<loginhistory> loginhistorylist = [select userid from loginhistory where logintime>:earlier and userid in:usd group by userid, hour_in_day(logintime) order by hour_in_day(logintime) desc limit :numactiveusers ];

for(loginhistory lh:loginhistorylist){
userid.add(lh.userid);
}
system.debug(user logged in count who is having test ps assigned-->+userid.size());

explanation of changes:

*

*removed id from the loginhistory select statement because its
not needed

*added group by userid, hour_in_day(logintime) and order by logintime descending to get the most recent login histories for each unique/grouped userid.

*you can avoid the 50001 error by limiting it by numactiveusers which is assigned value from an aggregate query


user
There are 79 "m"s in the text you provided.
assistant