0

I am trying to figure out a way to change the logic in default CommunitiesSelfRegController controller for user self registration via community. My use case is to create a new account everytime when a user is created via community. Can anyone please help me to achieve the same.

/**

* An apex page controller that supports self registration of users in communities that allow self registration */ public class CommunitiesSelfRegController {

public String firstName {get; set;}
public String lastName {get; set;}
public String email {get; set;}
public String password {get; set {password = value == null ? value : value.trim(); } }
public String confirmPassword {get; set { confirmPassword = value == null ? value : value.trim(); } }
public String communityNickname {get; set { communityNickname = value == null ? value : value.trim(); } }

public CommunitiesSelfRegController() {}

private boolean isValidPassword() {
    return password == confirmPassword;
}

public PageReference registerUser() {

       // it's okay if password is null - we'll send the user a random password in that case
    if (!isValidPassword()) {
        ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, Label.site.passwords_dont_match);
        ApexPages.addMessage(msg);
        return null;
    }    

    String profileId = '00e0n000000LvoX'; // To be filled in by customer.
    String roleEnum = null; // To be filled in by customer.
     // To be filled in by customer.

    Account Acc = new account();
    Acc.name = firstname + lastname;
    insert Acc;

    Contact c = new contact (Lastname = lastname);
    c.accountid = Acc.id;
    insert c;

    String accountId = Acc.id;
    String userName = email;

    User u = new User();
    u.Username = userName;
    u.Email = email;
    u.FirstName = firstName;
    u.LastName = lastName;
    u.CommunityNickname = communityNickname;
    u.ProfileId = profileId;

    String userId;

    try {
        userId = Site.createExternalUser(u, accountId, password);
    } catch(Site.ExternalUserCreateException ex) {
        List<String> errors = ex.getDisplayMessages();
        for (String error : errors)  {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, error));
        }

        // This message is used for debugging. Do not display this in the UI to the end user.
        // It has the information around why the user creation failed.
        System.debug(ex.getMessage());
    }

    if (userId != null) { 
        if (password != null && password.length() > 1) {
            return Site.login(userName, password, ApexPages.currentPage().getParameters().get('startURL'));
        }
        else {
            PageReference page = System.Page.CommunitiesSelfRegConfirm;
            page.setRedirect(true);
            return page;
        }
    }
    return null;
}

}

  • You have added code to create a new account every time. What is the error/problem you are facing? – RedDevil Jun 29 '17 at 5:36
  • Yes. I have added the code but the controller some how is not creating the new account and mapping the newly registered contact under the same account(This is the default functionality of the controller as the person account is not enabled in my org). Not sure if there is any setting which i am missing. Can you please help. – AJ007 Jun 29 '17 at 15:56
  • Can you increase logs and see if there is a exception or till which step your code is executing successfully. – RedDevil Jun 29 '17 at 15:57
  • Actually i tired to add Guest user to debug log but sf is not generating any logs for the guest user. Not sure if i am missing something. – AJ007 Jun 29 '17 at 16:03

Your Answer

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

Browse other questions tagged or ask your own question.