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
Sergio Ruiz BarriosSergio Ruiz Barrios 

Person Account in Customer Community

Hi,

I have person accounts created in my org and i need to give them the ability to self-register in our community. The problem is that they can´t register because of their existing email.
I only can register new users as person accounts with diferent emails and usernames.

How can i link a new user with his/her existing persoun account?

thanks!
Best Answer chosen by Sergio Ruiz Barrios
Shikha AgashiShikha Agashi
Hi Sergio, 

I am assuming you have unique account associated with each user record. When same user is trying to create new user, he/she is not able to create new user, as username is equal to email. so email has to unique. 

What I can suggest is, once user starts Registration form and enters Email, check immediately, whether that email is existing in your User DataBase. If it is already existing, then you can activate his user account and redirect to login page. Where they can retrieve password through Forgot Password Link.
 
String userName = email;

        User u = new User();
        u.Username = userName; //HERE YOU ARE INSERTING EMAIL OF USER  AND USERNAME IS UNIQUE ON USER RECORD. SO WHEN A SAME USER IS TRYING FOR SECOND TIME REGISTRATION PROCESS, HE IS GETTING ERROR, SAYING IT WOULD NOT BE CREATED
        u.Email = email;

.
.
.


// Answer to Last post.

Make Person Account records unique to each user record. To do so, please make sure personemail is unique, as you are using email to create username.

All Answers

Shikha AgashiShikha Agashi
Hi Sergio,

Is there any custom code that completes registration process? And is there a isActive field on your person account? What are unique field in your record type?
Sergio Ruiz BarriosSergio Ruiz Barrios
Hi Shikha,

The registration process is the Salesforce "out of the box" self-registration for communities, with the default self-registration page (CommunitiesSelfReg) and CommunitiesSelfRegController. I don´t have apex experience, so i don´t know if it is possible to customize this apex class and what i should add to be able to link the new users to their existing person accounts, instead of create new users with a new account.

There is not a isActive field in my person account, and i have a custom field (ID card) to identify each client.

Thanks for your reply!

Regards.
Shikha AgashiShikha Agashi
Hi Sergio, can you please paste page and controller code here? I need to look into that, what is actually stopping them if email is duplicate?
Sergio Ruiz BarriosSergio Ruiz Barrios
The Visualforce Page code:
<apex:page id="communitiesSelfRegPage" showHeader="true" controller="CommunitiesSelfRegController" title="{!$Label.site.user_registration}">
     <apex:define name="body">  
      <center>
<apex:form id="theForm" forceSSL="true">
                    <apex:pageMessages id="error"/>
                    <apex:panelGrid columns="2" style="margin-top:1em;">
                      <apex:outputLabel value="First Name" for="firstName"/>
                      <apex:inputText required="true" id="firstName" value="{!firstName}" label="First Name"/>
                      <apex:outputLabel value="Last Name" for="lastName"/>
                      <apex:inputText required="true" id="lastName" value="{!lastName}" label="Last Name"/>
                      <apex:outputLabel value="{!$Label.site.community_nickname}" for="communityNickname"/>
                      <apex:inputText required="true" id="communityNickname" value="{!communityNickname}" label="{!$Label.site.community_nickname}"/>
                      <apex:outputLabel value="{!$Label.site.email}" for="email"/>
                      <apex:inputText required="true" id="email" value="{!email}" label="{!$Label.site.email}"/>
                      <apex:outputLabel value="{!$Label.site.password}" for="password"/>
                      <apex:inputSecret id="password" value="{!password}"/>
                      <apex:outputLabel value="{!$Label.site.confirm_password}" for="confirmPassword"/>
                      <apex:inputSecret id="confirmPassword" value="{!confirmPassword}"/>
                      <apex:outputText value=""/>
                      <apex:commandButton action="{!registerUser}" value="{!$Label.site.submit}" id="submit"/>
                    </apex:panelGrid> 
                  <br/>
</apex:form>
     </center>
      <br/>
    </apex:define>

</apex:page>
Sergio Ruiz BarriosSergio Ruiz Barrios
And the controller:
/**
 * 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 = null; // To be filled in by customer.
        String roleEnum = null; // To be filled in by customer.
        String accountId = null; // To be filled in by customer.
        
        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;
    }
}
Sergio Ruiz BarriosSergio Ruiz Barrios
The problem is almost resolved, but if i register a person account that is already created before in salesforce (and not enabled as a community user) a new person account with the same name and email is created (Duplicate record).
How we can avoid this?

Thanks, I really appreciate your help!
Shikha AgashiShikha Agashi
Hi Sergio, 

I am assuming you have unique account associated with each user record. When same user is trying to create new user, he/she is not able to create new user, as username is equal to email. so email has to unique. 

What I can suggest is, once user starts Registration form and enters Email, check immediately, whether that email is existing in your User DataBase. If it is already existing, then you can activate his user account and redirect to login page. Where they can retrieve password through Forgot Password Link.
 
String userName = email;

        User u = new User();
        u.Username = userName; //HERE YOU ARE INSERTING EMAIL OF USER  AND USERNAME IS UNIQUE ON USER RECORD. SO WHEN A SAME USER IS TRYING FOR SECOND TIME REGISTRATION PROCESS, HE IS GETTING ERROR, SAYING IT WOULD NOT BE CREATED
        u.Email = email;

.
.
.


// Answer to Last post.

Make Person Account records unique to each user record. To do so, please make sure personemail is unique, as you are using email to create username.
This was selected as the best answer
Shikha AgashiShikha Agashi
What fix did you add? 
Sergio Ruiz BarriosSergio Ruiz Barrios
I will try that.

My problem was that i didn't add the person account record type in the public access site (Guest User Profile)!

Thank you so much!
Sergio Ruiz Barrios 5Sergio Ruiz Barrios 5
I have created person accounts in salesforce (but not enabled as customer users), and i need to link new users that are self registered in the community with their existing person account.

The problem is that when they are registered, a new person account is created with the same name and email (duplicated record). How can i fix this?