You need to sign in to do that
Don't have an account?
Sahaya Steffi J
Trigger on User
Help me with a trigger on user when a active checkbox field is unchecked(inactive), then the user with the same email id should also be inactive and should append with '.inactive' (i.e) [Username = Contact Email +.inactive]
I have gone through your question you can make the trigger on the user. The trigger will work on after update. Please follow the below code for your use case.
Trigger ===>>
trigger UserTrigger on User (after update) {
if(trigger.isAfter && trigger.isUpdate){
UserHandler.inactiveUser(trigger.new);
}
}
Handler Class ===>>
public class UserHandler {
public static void inactiveUser(List<User> userlist){
try{
//Set<Id> uid = new Set<ID>();
List<String> userEmail = new List<String>();
for(User u : userlist){
if(u.IsActive == false){
userEmail.add(u.Email);
}
}
List<User> ulist = [Select id,Username,Email,IsActive from user where Email in : userEmail ];
System.debug('ulist'+ulist);
List<User> updtUser = new List<User>();
for(User u : ulist){
u.IsActive = false;
u.Username = u.Username + 'inactive';
updtUser.add(u);
}
update updtUser;
}
catch(Exception e){
System.debug('Line No:-->'+e.getLineNumber()+'Error--->'+e.getMessage()+'Cause--->'+e.getCause());
}
}
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com