You need to sign in to do that
Don't have an account?
sam_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';
}
}
}
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
All Answers
Hi,
Not sure the API version you are using but you should do this:
Hope that helps
Joao
PS: Please mark as solution if it fixed your problem
Iam using version 25, but the below one doesn't works the role is not still changing to Inactive
Try this.
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
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:
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
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
Perfect it's working exactly the way i want
Thanks Jose