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
ezhil_kezhil_k 

Query on User object

I want to make a inactive user to active.

 

Is it possible to query on User object which is having n number of inactive users, and get a particular user and make active?

CheyneCheyne

Try this

 

User u = [SELECT Id, IsActive FROM User WHERE username = 'some username'];
u.isActive = True;
update u;

 

Gaurish Gopal GoelGaurish Gopal Goel

Try this code. This can be used for bulk records. You can accept this as a solution if this solves your problem.

 

List<User> user_list = [Select id,isActive from user where isActive=False];

List<User> new_Users = new List<User>();

For(User u : user_list)

{

u.isActive = True;

new_Users.add(u);

}

update new_Users;

Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi Ezil,

 

You can do the updation in user object, before that you have to think of which users based on some conditions, you need to update as active.

 

So, you need to get the user list those who are inactive.

 

List<User> inactiveUsers = [SELECT Id,name,ProfileId,IsActive FROM User WHERE isActive =: false ];

List<Users> ModifiedUsersList = new List<Users>();

if(inactiveUsers.size() > 0){

      for(User u : inactiveUsers){
           //Check the condition for the users whom you are going to make active again. If you are going to make the all the users active those who are inactive you don't need this IF condition here
            if(u.id == <your id to check here> || u.ProfileId == <check profile>){
                   u.isActive = true;
                   ModifiedUsersList.add(u);
            }
        }
      update ModifiedUsersList;
}

 

 

Hope so this helps you...!

Please mark this answer a Solution and please give kudos by clicking on the star icon, if you found this answer as helpful.

CheyneCheyne

Rather than pulling a list of all inactive users and then looping through them to find the ones you want to update, you should be able to add conditions in your query to pull exactly the users that you wish to update. This should make your code more efficeint. Also, I would be careful using IDs as criteria, because they will differ between your sandbox and production environments.

 

It might be helpful if you explained the criteria that you are using to determine which users to activate. Then, we can determine exactly what SOQL query you need to get the correct list of users.