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
sam_Adminsam_Admin 

Set the Role to Inactive when you deactivate the user

Hi,

  So whenever i deactivate any user i want to set the role to inactive since we can't do this through workflow i wrote trigger but when i deactivate the user the role doesn't seems to get changed any ideas?

 

trigger Inactive on User (after update) {
list<User> ulist = [select u.Id,U.Name,u.IsActive,u.IsBmEnabled__c,u.UserRoleId,u.UserRole.Name from User u where u.Id =: trigger.new[0].Id];

 for(User u:ulist){

   if(u.Isactive==false){

      u.IsBMEnabled__c = false;

      u.UserRole.Name = 'Inactive';

      }

 }

}

Best Answer chosen by Admin (Salesforce Developers) 
Jerun JoseJerun Jose

Aren't you trying to reassign the deactivated users to the 'Inactive' role? Some posts over here seem to be updating the deactivated user's current role to rename that role to be 'Inactive'. i.e changing the role details instead of the role alignment.

If you want to reassign the user's to the inactive role. In a before update trigger you will have to change the UserRoleID for the user record that you are working. It would be best to query for this role ID, although you could get away with hardcoding

 

trigger changeRole on User(before update){
	ID deactiveRoleID = [select id from UserRole where Name = 'Inactive'].ID;
	// or harcode as ID deactiveRoleID = 'xxxxx';
	for(User usr : trigger.new){
		if(usr.IsActive == false && trigger.oldmap.get(usr.ID).IsActive == true)
			usr.UserRoleID = deactiveRoleID;
	}
}

 

All Answers

stunaitestunaite

Hi,

 

Not sure the API version you are using but you should do this:

 

trigger Inactive on User (after update) {
list<User> ulist = [select u.Id,U.Name,u.IsActive,u.IsBmEnabled__c,u.UserRoleId,u.UserRole.Name from User u where u.Id =: trigger.new[0].Id];

 for(User u:ulist){

   if(u.Isactive==false){

      u.IsBMEnabled__c = false;

      u.UserRoleId = [select id from UserRole where name = 'Inactive'].id;

      }

 }

}

 

Hope that helps

 

Joao

 

PS: Please mark as solution if it fixed your problem

sam_Adminsam_Admin

Iam using version 25, but the below one doesn't works the role is not still changing to Inactive

Naidu PothiniNaidu Pothini
trigger Inactive on User (before update) 
{
	for(User u : Trigger.new)
	{
		if(u.Isactive == false)
		{
		  u.IsBMEnabled__c = false;
		  u.UserRole.Name = 'Inactive';
		}
	}
}

 Try this.

stunaitestunaite

Hi Again.

 

Your problem is you aren't saving the user (it is already done) with the new role. If you try to do you'll get an exception.

 

You have to use a before update trigger.

 

 

Hope that helps

 

Joao

 

PS: Please mark as solution if it fixed your problem

Leon MorockiLeon Morocki

Hi,

 

As far as I remember if you have a trigger for e.g. User - only felds that belong to User object are going to be commited to the database by default. Any fields from the related objects need to be committed manually.

 

So you would probably want to use:

 

update u.UserRole;

 

sam_Adminsam_Admin

Joao: Before update is not working either :(

Leon: I used update u.UserRole; but still no difference i was guesing the samething that we got to do it manually but i was hoping if there is an process to do this automatically 

Jerun JoseJerun Jose

Aren't you trying to reassign the deactivated users to the 'Inactive' role? Some posts over here seem to be updating the deactivated user's current role to rename that role to be 'Inactive'. i.e changing the role details instead of the role alignment.

If you want to reassign the user's to the inactive role. In a before update trigger you will have to change the UserRoleID for the user record that you are working. It would be best to query for this role ID, although you could get away with hardcoding

 

trigger changeRole on User(before update){
	ID deactiveRoleID = [select id from UserRole where Name = 'Inactive'].ID;
	// or harcode as ID deactiveRoleID = 'xxxxx';
	for(User usr : trigger.new){
		if(usr.IsActive == false && trigger.oldmap.get(usr.ID).IsActive == true)
			usr.UserRoleID = deactiveRoleID;
	}
}

 

This was selected as the best answer
sam_Adminsam_Admin

Perfect it's working exactly the way i want

 

Thanks Jose