Notes
public class UserUtility {
public static User getUserById(String userId){
return [Select Id, Username, Email From User Where Id = :userId];
}
public static List<User> getUsersByEmail(String email){
try{
List<User> users = [Select Id, FirstName, LastName, UserName From User Where Email =:email];
System.debug(users);
return users;
}catch(Exception e){
System.debug(e.getMessage());
return new List<User>{};
}
}
public static List<User> getUsersByProfile(String profileName){
try{
Profile pr = [Select Id, Name From Profile Where Name=:profileName Limit 1];
List<User> users = [Select Id, FirstName, LastName, UserName From User Where ProfileId =: pr.Id ];
System.debug(users);
return users;
}catch(Exception e){
System.debug(e.getMessage());
return new List<User>();
}
}
public Static List<User> getUsersByRole(String roleName){
try{
UserRole ur = [Select Id, Name From UserRole Where Name =:roleName Limit 1];
List<User> users = [Select Id, UserName From User Where UserRoleId =: ur.Id];
System.debug(users);
return users;
}catch(Exception e){
System.debug(e.getMessage());
return new List<User>();
}
}
public static User getSessionUser(){
String sessionUserId = UserInfo.getUserId();
User user = [Select Id, UserName, Email From User Where Id=:sessionUserId];
System.debug(user);
return user;
}
public static List<User> getActiveUsers(){
try{
List<User> activeUsers =[Select Id, Username, Email From User Where isActive= true];
system.debug(activeUsers);
return activeUsers;
}catch(Exception e){
System.debug(e.getMessage());
return new List<User>();
}
}
public static void resetUserPasswordByUserId(String userId, String newPassword){
try{
System.resetPassword(userId, true);
}catch(Exception e){
System.debug(e.getMessage());
}
}
public static void SetUserPasswordByUserId(String userId, String newPassword){
try{
System.setPassword(userId, newPassword);
}catch(Exception e){
System.debug(e.getMessage());
}
}
public static List<User> GetUsersByManager(String managerId){
try{
List<User> users =[Select Id, UserName, ManagerId From User Where ManagerId =: managerId];
System.debug(users);
return users;
}catch(Exception e){
System.debug(e.getMessage());
return new List<User>{};
}
}
public Static List<User> getInactiveUsers(){
try{
List<User> inactiveUsers =[Select Id, Username, Email From User Where isActive= false];
system.debug(inactiveUsers);
return inactiveUsers;
}catch(Exception e){
return new List<User>{};
}
}
public static void createAUser(String firstName, String lastName, String Email, String profileName){
Profile pr = [Select Id, Name,UserLicense.Name From Profile Where Name = :profileName Limit 1];
UserLicense ul = [Select Id, TotalLicenses , UsedLicenses From UserLicense Where id =:pr.UserLicenseId ];
Integer remainingLicenses = ul.TotalLicenses - ul.UsedLicenses;
String companyName = [Select Name From Organization Limit 1].Name;
String username = firstName.substring(0,1).tolowerCase() + lastname.toLowerCase() +'@'+ companyName.tolowerCase() +'.org';
String alias = firstName.substring(0,3).tolowerCase() + lastName.substring(0,3).tolowerCase();
try{
if(remainingLicenses>0){
User user = new User();
user.FirstName = firstName;
User.LastName = lastName;
user.Username = username;
user.ProfileId = pr.Id;
user.Alias = StringUtility.createAnAliasforUser(firstName, lastName);
user.email = Email;
user.TimeZoneSidKey='America/Lima';
user.LocaleSidKey='en_US';
user.EmailEncodingKey='UTF-8';
user.LanguageLocaleKey='en_US';
insert user;
}
}Catch(Exception e){
System.debug(e.getMessage());
}
}
public static void MassUpdateOfUserRolesToNull(){
List<User> users = [Select id, UserRoleId From User Where UserRoleId!= null];
List<User> usersToBeUpdated = new List<User>();
if(users.size()!=0){
for(User u: users){
u.UserRoleId =null;
usersToBeUpdated.add(u);
}
}
update usersToBeUpdated;
}
public static List<User> getUsersByPublicGroup(String groupName) {
try {
Group gr = [SELECT Id, Name,Type FROM Group WHERE Name = :groupName AND Type='Public' LIMIT 1];
List<GroupMember> groupMembers = [Select UserOrGroupId FROM GroupMember WHERE GroupId = :gr.Id AND UserOrGroupId != null];
List<Id> UserIds = new List<Id>();
for(GroupMember member : groupMembers){
UserIds.add( member.UserOrGroupId);
}
List<User> usersInAPublicGroup = [Select Id, Name From User Where Id In :UserIds];
return usersInAPublicGroup;
} catch (Exception e) {
System.debug('Error: ' + e.getMessage());
return new List<User>();
}
}
public static List<User> getUsersAssignedToAPermissionSet(String permissionSetName) {
try {
PermissionSet ps = [SELECT Id, Name FROM PermissionSet WHERE Name = :permissionSetName LIMIT 1];
List<PermissionSetAssignment> psa = [SELECT Id, AssigneeId FROM PermissionSetAssignment WHERE PermissionSetId = :ps.Id];
Set<Id> ids = new Set<Id>();
for (PermissionSetAssignment assignment : psa) {
ids.add(assignment.AssigneeId);
}
List<User> users = [SELECT Id, Name FROM User WHERE Id IN :ids];
return users;
} catch (Exception e) {
System.debug('Error: ' + e.getMessage());
return new List<User>();
}
}
public static List<User> GetUsersByTimeZone(String timeZone){
try{
List<User> usersFromSameTimeZone = [Select Id, Name From User Where TimeZoneSidKey =:timeZone];
return usersFromSameTimeZone;
}catch(Exception e){
System.debug('Error: ' + e.getMessage());
return new List<User>();
}
}
public static void updateUserNameById(String userId, String firstName, String lastName ){
try{
List<User> users= [Select Id, FirstName , LastName From User Where FirstName =:firstName AND LastName =:lastName];
if(users.size()==0){
User user = [Select FirstName , LastName From User Where Id =:userId];
user.FirstName = firstName;
user.LastName = lastName;
update user;
}else{
System.debug('There is already a User with the name of '+ firstName +' '+ lastName + '.' );
}
}catch(Exception e){
System.debug('Error: ' + e.getMessage());
}
}
public static List<User> GetUsersBasedOnLastLogin(Date loginDate){
try {
List<User> users = [Select Id, Name From User Where LastLoginDate =:loginDate];
System.debug(users);
return users;
} catch (Exception e) {
System.debug('Error: ' + e.getMessage());
return new List<User>();
}
}
public static List<User> GetFrozenUserWay1(){
try {
List<UserLogin> UserLoginList = [SELECT Id, UserId,isFrozen FROM UserLogin WHERE isFrozen=true];
system.debug(UserLoginList);
List<Id> frozenUsersIds = new List<Id>();
for(UserLogin login: UserLoginList){
frozenUsersIds.add(login.UserId) ;
}
List<User> frozenUsers =[Select Id, Name From User Where Id IN :frozenUsersIds];
System.debug(frozenUsers);
return frozenusers;
} catch (Exception e) {
System.debug('Error: ' + e.getMessage());
return new List<User>();
}
}
public static List<User> GetFrozenUsersWay2(){
return [Select Id, Name From User Where Id IN (SELECT UserId FROM UserLogin WHERE isFrozen=true)];
}
}