0

I have below trigger that needs to activate the User as well as generate reset password link for the same when a custom checkbox field is checked. Below is my apex trigger code that throws this error on an inactive user's detail page as I try to Save the checkbox checked. Basically making Isactive field of User as checked through trigger is not happening. Can someone please help.

Apex Trigger:

trigger TrainingCompleted on User (before update) {
    for (User updated : Trigger.new) {
        if(updated.Training_Complete_abv__c = true)
        {   updated.IsActive =true;
           if( updated.IsActive =true)
            System.resetPassword(updated.Id,true);// error line

        }
    }
}

Error:

 execution of BeforeUpdate caused by: System.UnexpectedException: INACTIVE_OWNER_OR_USER: owner or user is inactive. Org Id:00D8A0000008i2Q User Id:005F0000003mdWV: Trigger.TrainingCompleted: 

1 Answer 1

2

Its because you are trying to reset the password in before update context when the user is still Inactive.

Try to reset the password in after update. That should work I guess.

EDIT

Have not tested the code. Just wrote it on the fly. So might have issues, but should get you started.

trigger TrainingCompleted on User (before update, after update) {
    for (User updated : Trigger.new) {
    if(Trigger.isBefore){
            if(updated.Training_Complete_abv__c = true){   
           updated.IsActive =true;
        }
    }
    else if(Trigger.isAfter){
           if(!Trigger.oldMap.get(updated.id).isActive && Trigger.newMap.get(updated.id).isActive)
            System.resetPassword(updated.Id,true);
    }

    }
}
4
  • What would be the code? Update statement shd be there called explicitly? Commented Mar 31, 2016 at 9:54
  • Getting this error : Review all error messages below to correct your data. Apex trigger TrainingCompleted caused an unexpected exception, contact your administrator: TrainingCompleted: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 005F0000003mdWVIAY; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, TrainingCompleted: maximum trigger depth exceeded .... Commented Mar 31, 2016 at 10:00
  • 1
    I added some code. Did you try to explicitly do an update again on the User? If so then NOOOOOO. The reset password does something internally and does not change anything on the User.
    – AslamK
    Commented Mar 31, 2016 at 10:04
  • Hey got it man!!! Love new things to learn!! Commented Mar 31, 2016 at 10:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .