+ Start a Discussion
BohaoBohao 

Site.createPortalUser() always return null userId

I have a site for self-registration which calls Site.createPortalUser()  to create a new portal user if it is a new user. It works perfectly on site registration page. The problem now is that I have to capture facebook login and create portal user for facebook users. Current code has callout function to get facebook user's email, userid and username, etc. And then, pass those info to the same function used for site registration page. All parameters are correct, just don't know why Site.createPortalUser()  always give me null userId? Anyone has any idea about this?

Best Answer chosen by Admin (Salesforce Developers) 
BohaoBohao

Problem solved. 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.

All Answers

IspitaIspita

 

Hi Bohao,

The function "createPortalUser" - needs the following parametrs:-

  • sObject user - in the user pbject passed the nickname field is required
  • String accountId - required 
  • String opt_password - optional
  • Boolean opt_sendEmailConfirmation - optional 
  • The fact that null is returned suggests that your user is not getting created reason can be absence of accountid  or nickname field of user parameter passed

Creates a portal user for the given account and associates it with the site's portal.

The optional opt_password argument is the password of the portal user. If not specified, or if set to null or an empty string, this method sends a new password email to the portal user.

The optional opt_sendEmailConfirmation argument determines whether a new user email is sent to the portal user. Set it to true to send a new user email to the portal user. The default is false, that is, the new user email isn't sent.

The nickname field is required for the user sObject when using thecreatePortalUser

 method.

 

Note:  This method is only valid when a site is associated with a Customer Portal.Refer to the sample code for clarity:-
public PageReference registerUser() {
        // If password is null, a random password is sent to the user 
    
        if (!isValidPassword()) {
           ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, 
               Label.site.passwords_dont_match);
           ApexPages.addMessage(msg);
            return null;
        }    
        User u = new User();
        u.Username = username;
        u.Email = email;
        u.CommunityNickname = communityNickname;
        
        String accountId = PORTAL_ACCOUNT_ID;

        // lastName is a required field on user, but if it isn't specified,  
    
           the code uses the username
        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.SiteRegisterConfirm;
                page.setRedirect(true);
                return page;
            }
        }
        return null;
    }

 

Hope this helps....
BohaoBohao

Hi Ispita 

 

Thank you for replying me. I am using same same code for my project. And I checked all parameters including nickname and accountId, and left password empty string. Basically, the parameters I am using are in the same format as site registration page. And there is no duplicate user that has been created before. The only difference is that registration page has 'Register' button to call createPortalUser() function and facebook login is calling createPortalUser() inside another apex function. 

BohaoBohao

Problem solved. 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.

This was selected as the best answer
Chirag MehtaChirag Mehta

Thanks, this helped.

vanessenvanessen

i am creating from trigger and it is nor working.Is it the issue of forceSSL??

vanessenvanessen

for my case, i am creating portal user in trigger, so i am not able to use the site.createportalUser, si i re-implemented the standard function.

sharmapankaj133sharmapankaj133

In that case how to create user by javascript remoting? if we are using an html form instead of apex form

srashti jain 10srashti jain 10
I need to achieve 86% coverage for this class but I reached till 76% please help--

public with sharing class SiteRegisterController {
    // PORTAL_ACCOUNT_ID is the account on which the contact will be created on and then enabled as a portal user.
    // you need to add the account owner into the role hierarchy before this will work - please see Customer Portal Setup help for more information.       
    @TestVisible
    private static Id PORTAL_ACCOUNT_ID = '001x000xxx35tPN';
    
    public SiteRegisterController () {
    }

    public String username {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(); } }
      
    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;
        }    
        User u = new User();
        u.Username = username;
        u.Email = email;
        u.CommunityNickname = communityNickname;
        
        String accountId = PORTAL_ACCOUNT_ID;

        // lastName is a required field on user, but if it isn't specified, we'll default it to the username
        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.SiteRegisterConfirm;
                page.setRedirect(true);
                return page;
            }
        }
        return null;
    }
}
Aniket KushwahaAniket Kushwaha
hello srashti jain 10,
i know its late but for helping others i will try to answer this,
so what you can do is:
as site.createPortalUser will return null so you can apply a condition like:

 String userId = Site.createPortalUser(u, accountId, password);
        if(test.isRunningTest()){
            userid='temp';
        }
        if (userId != null) { 
            if (password != null && password.length() > 1) {
                return Site.login(username, password, null);
            }
            else {
                PageReference page = System.Page.SiteRegisterConfirm;
                page.setRedirect(true);
                return page;
            }

and it will help to increase the coverage