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
NVN DevNVN Dev 

Site.createExternalUser(u, accountId, password) returning null

Hello All,

 While I am creating community through apex, Site.createExternalUser(u, accountId, password) is returning null

 Can anyone help me out?
James LoghryJames Loghry
I'm not sure what your setup is exactly, but this post states you should use forceSSL=true on your apex:form to ensure you're creating the user over a secure request.  https://developer.salesforce.com/forums/?id=906F00000008zxtIAA
NVN DevNVN Dev
Hi James,

   I have used, forceSSL=true.

   Is there anything else which i need to be looked at?

Thanks,
Naveen
James LoghryJames Loghry
Only other thing I would look at is if you're catching any exceptions thrown from the createExternalUser call and "swallowing" them or keeping them from bubbling up.  Do you have the call wrapped in a try catch?  If so, I would check to see if it's throwing an exception.
NVN DevNVN Dev
Yes, I have wrapped in try catch.

 try {
            System.debug('--in try---');
            System.debug('---user---'+u);
            userId = Site.createExternalUser(u, accountId, password);
            System.debug(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));
            }
            System.debug(ex.getMessage());
        }

 
NVN DevNVN Dev
But its not throwing any exception as of now
James LoghryJames Loghry
You're calling Site.createExternalUser twice.  You should only call it once.  Instead of
 
System.debug(Site.createExternalUser(...));
You should debug the Id with:
 
System.debug(userId);

That being said, it's likely you already created the user if you've been testing this awhile.  Add some System.debugs to the catch bracket as well.  Try debugging ex.getMessage() to see if that gives you anything.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_Site_exceptions.htm
NVN DevNVN Dev
I have made the changes suggested by you, though still its returniung null

public PageReference registerUser() {
        System.debug('--method called---');
           // it's okay if password is null - we'll send the user a random password in that case
        

        String profileId = '00e28000000x7rk'; // To be filled in by customer.
        String roleEnum = 'Customer User'; // To be filled in by customer.
        String accountId = '001280000093OaC'; // To be filled in by customer.
        
        String userName = email;

        User u = new User();
        u.Username = 'naveen@portal.com';
        u.Email = 'naveen.sfdc21@gmail.com';
        u.FirstName = 'Naveen';
        u.LastName = 'Kakrla';
        u.CommunityNickname = 'naveen.sf21';
        u.ProfileId = profileId;
        
        String userId;

       
        try {
            System.debug('--in try---');
            System.debug('---user---'+u);
            userId = Site.createExternalUser(u, accountId, password);
            System.debug(userId);
        } catch(Site.ExternalUserCreateException ex) {
            System.debug('--catch---');
            List<String> errors = ex.getDisplayMessages();
            for (String error : errors)  {
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, error));
            }
            System.debug(ex.getMessage());
        }
        
        if (userId != null) { 
            if (password != null && password.length() > 1) {
                System.debug('--in if password---');
                return Site.login(userName, password, ApexPages.currentPage().getParameters().get('startURL'));
            }
            else {
                System.debug('--in else password---');
                PageReference page = System.Page.CommunitiesSelfRegConfirm;
                page.setRedirect(true);
                return page;
            }
        }
        return null;
    }
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. 

createExternalUser will not throw an exception, but will just return null in this case.

Credit goes to: AvailableName (https://salesforce.stackexchange.com/a/228106/53505)
Nagarjuna SadhuNagarjuna Sadhu
Hi Cagdas,

Are you write the test class the above code. I am writing test calss it covers only 76% , Can any one have to write the test class,
Please share the comment below.