+ Start a Discussion
sai man 1sai man 1 

Site.createPortalUser () is returning null

I am trying to implement the 'Allow external users to self-register' for external users of my community portal. I am using the standard page CommunitiesSelfRegConfirm and the associated controller,but Site.createPortalUser () is returning null. I have attached te corresponding VF nd Apex , basically they are the default communityself registration pages.Please provide your valuable imputs in resolving this.
 
VF
======
<apex:page id="communitiesSelfRegConfirmPage" controller="CommunitiesSelfRegConfirmController" showHeader="true" cache="false" title="{!$Label.site.registration_confirmation}" >
    <apex:form forceSSL="true"> 
    <apex:define name="body">  
      <center>
        <apex:panelGrid bgcolor="white" columns="1" style="align: center;"> 
          <br/>
          <br/>
          <apex:panelGrid width="758" cellpadding="0" cellspacing="0" bgcolor="white" columns="1" style="topPanel"> 
            <br/>
            <apex:outputPanel layout="block" styleClass="topPanelContainer">
              <apex:panelGrid width="758" cellpadding="0" cellspacing="0" bgcolor="white" columns="2"> 
                <apex:image url="{!URLFOR($Resource.SiteSamples, 'img/clock.png')}"/>
                <apex:panelGroup >
                  <br/>
                  <apex:outputText styleClass="title" value="{!$Label.site.thank_you_for_registering}"/>
                  <br/>
                  <br/>
                  <apex:outputLink value="{!$Page.CommunitiesLogin}" rendered="{!$Site.LoginEnabled}">{!$Label.site.go_to_login_page}</apex:outputLink>
                  <br/>
                </apex:panelGroup>
              </apex:panelGrid> 
            </apex:outputPanel>
            <c:SitePoweredBy />
          </apex:panelGrid> 
       </apex:panelGrid>
      </center>
      <br/>
    </apex:define>
    </apex:form>
</apex:page>
 
Apex
======
/**
 * 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 = '00e90000001BjbTAAS'; // To be filled in by customer.
        String roleEnum = null; // To be filled in by customer.
        String accountId = '0019000001K0ObF'; // To be filled in by customer.
        System.debug('profileId '+profileId +'roleEnum'+roleEnum +'accountId'+accountId);
        String userName = email;
        String userId=null;
        User u = new User();
        u.Username = userName;
        u.Email = email;
        u.FirstName = firstName;
        u.LastName = lastName;
        u.CommunityNickname = communityNickname;
        u.ProfileId = profileId;
        System.debug('check here'+userName+','+email+firstName+firstName+communityNickname+','+profileId);
        try{
        userId = Site.createPortalUser(u,'  0019000001K0ObFAAV', password);
        }
        catch(Exception e)
        {
        System.debug('ERROR:' + e);
        }
        System.debug('this is it'+userId );
        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;
    }
}

 
NagaNaga (Salesforce Developers) 
Hi Sai,

In order to call createportaluser function, caller should be inside a form with forceSSL=true. otherwise, userid will return null when u call createportaluser function.

Please let me know if this helps

Best Regards
Naga kiran
sai man 1sai man 1
Hi Naga,

Thanks for the reply.forceSSL=true in the vf page from where the user is trying to self register , its the default login page for Communities self registration user namely CommunitiesSelfReg , but still am facing the issue.
sai man 1sai man 1
Still the issue persists, any help will be much appreciated
Cagdas Cubukcu 17Cagdas Cubukcu 17
I had the same problem and sharing the my solution in case anybody comes to this question.

The problem for me was entering a password that does not match the org password requirements. 

Credit goes to: AvailableName (https://salesforce.stackexchange.com/a/228106/53505)