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
KevinSnellKevinSnell 

Customer Portal Self-Registration - Create account rather link to one

Hi all,

 

So I've been searching for an answer but not come up with an answer so hoping the experts here can help.


Currently with Communities Self Registration the contact record related to the user which is created on registration is current linked to one account set in the CommunitiesSelfRegController (see below):

 

/**
 * An apex page controller that supports self registration of users in communities that allow self registration
 */
public with sharing 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 = ''; // 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 = Site.createPortalUser(u, accountId, password);
      
        if (userId != null) { 
            if (password != null && password.length() > 1) {
                return Site.login(userName, password, null);
            }
            else {
                PageReference page = System.Page.CommunitiesSelfRegConfirm;
                page.setRedirect(true);
                return page;
            }
        }
        return null;
    }
}

 

My question is:  Has anybody modified the controller so an account is created for each contact when the user registers?

 

Thanks

Kev

Satyendra RawatSatyendra Rawat

Hi,

 

First create Account and contact, If Account or contact already exits,

 

split the username and get the domain name of the Register user, and then fetch the record and assion the accountId

 

Site.createPortalUser(u, accountId, password);

 

 

RoshRosh

Hi,

 Yes we can modify the controller to create seperate account for each User.

 

Try using the following sample code:

 

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

 

/*----Here you can create a seperate Account for each user------*/

Account account = new Account();
account.Name = firstName + ' ' + lastName;
insert account;

 

/*----Assign the newly created Account Id to accountId-----*/
String accountId = account.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 = Site.createPortalUser(u, accountId, password);

 

 

Syed Subhan 9Syed Subhan 9
Hi Rosh,

During the sites self-registration process, I have only come across the ability to create a portal user that creates both a contact and a user synchronously.  I would like to be able to create a portal user using an existing contact. 

So I am looking for a way to auto match users to their contacts to their user records if the email addresses match.  I understand that this can done with a controller.  I don't do that level of SF work yet, so any help would be great.

Can u post the code in which you were able to enable new users under existing contact. It will help me alot...

I need to modify the default self registration code to work like creating new users with existing contacts.
/**
 * An apex page controller that supports self registration of users in communities that allow self registration
 */
public with sharing 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.
       <strong> String accountId = ''; // To be filled in by customer.</strong>
        
        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 = Site.createPortalUser(u, accountId, password);
      
        if (userId != null) { 
            if (password != null && password.length() > 1) {
                return Site.login(userName, password, null);
            }
            else {
                PageReference page = System.Page.CommunitiesSelfRegConfirm;
                page.setRedirect(true);
                return page;
            }
        }
        return null;
    }
}