4

I have created a user from community. And from that community i need to reset password by Apex code.

Right now i am trying call System.resetpassword() method to do this but no success.

Exception is:

INSUFFICIENT_ACCESS: portal user email settings are not available

Exception seems self descriptive but I need to reset password of user programatically. Is it possible and in which scenarios?

0

I also get this error and when I went to community and allow the profile on community. This profile which you are giving to user. and I am using web service in my context.

I think this wll help you.

1

I was able to resolve a similar problem with System.resetPassword() by ensuring that this call is made from a class declared WITHOUT SHARING. The reason it works is because this class is executing in the System Context with elevated permissions.

/**
 * An apex page controller that exposes the site forgot password functionality
 * but performs a hard reset on the existing password to prevent further logins without a reset
 */
public with sharing class ForgotPasswordHardResetController {
    // Username is set by the Visual Force page
    public String username {get; set;}   

    public ForgotPasswordHardResetController() {}

    public PageReference forgotPassword() {         
        // Reset Password Requires the Userid
        try {
          List<User> matchingUsers = [SELECT id,username,email from User where UserName = :username];
            if (matchingUsers.size()>0) {
               User currentUser = matchingUsers[0];

               // Method is delegated to another class which uses without sharing to workaround permissions
               new ResetHelper().resetPassword(currentUser);

               PageReference pr = Page.ForgotPasswordConfirm;
               pr.setRedirect(true);
               System.debug('Password reset initiated');
               return pr;       
            } else {
               ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, 'Username not found');
               ApexPages.addMessage(msg);
               return null; 
            }
        } catch (System.Exception ex) {
            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, 'Unexpected error');
            ApexPages.addMessage(msg);
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,ex.getMessage()));
            return null;
        }


    }

    /**
     * Helper class must be declared without sharing to force it to run in System Context
     **/ 
    public without sharing class ResetHelper {
        public void resetPassword(User currentUser) {
            System.resetPassword(currentUser.id,false); // force immediate password reset, no email
            Site.forgotPassword(currentUser.Username);  // repeat the reset and send the templated community email
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.