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
Sahaya Steffi JSahaya 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]
Deepali KulshresthaDeepali Kulshrestha
Hi Sahaya,

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 
 
Sahaya Steffi JSahaya Steffi J
Thanks Deepali