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
vishal reddy 20vishal reddy 20 

apex class to print no of users with roleId on log file ex: abc roleId: 2 xyz: 4

Best Answer chosen by vishal reddy 20
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi vishal,
Try below code.I have tried this in my org.Please modify code according to your requirement.
public class userRoles {
    list<user> ulist = new list<user>();
    public void getuserRolebyId(){
        List<AggregateResult>  ulist = [SELECT UserRoleId , count(Id)idsCount  FROM User group by UserRoleId];
        for(AggregateResult a : ulist){
            if(a.get('UserRoleId')!= null)
            system.debug(a.get('UserRoleId') +':'+ a.get('idscount'));
        }
    }
    
}

Hope this helps you
If this helps kindly mark it as solved so that it may help others in future.

Thanks and Regards

All Answers

Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi vishal,
Try below code.I have tried this in my org.Please modify code according to your requirement.
public class userRoles {
    list<user> ulist = new list<user>();
    public void getuserRolebyId(){
        List<AggregateResult>  ulist = [SELECT UserRoleId , count(Id)idsCount  FROM User group by UserRoleId];
        for(AggregateResult a : ulist){
            if(a.get('UserRoleId')!= null)
            system.debug(a.get('UserRoleId') +':'+ a.get('idscount'));
        }
    }
    
}

Hope this helps you
If this helps kindly mark it as solved so that it may help others in future.

Thanks and Regards
This was selected as the best answer
vishal reddy 20vishal reddy 20
Above code works. 

Thanks and Regards