Any documentation on the CreatePortalUser ? In few cases it does not create community user and do not see any reason why. There is only 1 contact that is passed to it.

system.debug(' test user' +u); 
system.debug(' test account'+cons[0].accountid); 
String userId = Site.createPortalUser(u, cons[0].accountId, password);

I guess the question is does Salesforce create the contact when it creates the portal user? What I am doing, is setting the contact on the user before I call the method. This works most of the time, SF does not create duplicate contact. However there are certain cases where this fails. Sometime this is due to duplicate contacts even though the specific contact is set on the user record. The other time when there is only 1 contact I do not understand why it fails :(

Also forgot to mention that when this fails I am able to manually create the portal user for the contact. So no clue as to why the self registration failed. I am assigning random number to nickname. Hopefully SF will be nice enough to send me an email.

  • What is your specific question, or can you post your createUser() code?
    – greenstork
    Jul 31, 2014 at 19:37
  • system.debug(' test user' +u); system.debug(' test account'+cons[0].accountid); String userId = Site.createPortalUser(u, cons[0].accountId, password);
    – addy
    Jul 31, 2014 at 20:07
  • Hey @addy, I moved your code into the question to make it clearer. You can always edit your question to add to it, by clicking the edit link. Aug 1, 2014 at 1:04
  • Appreciate the tip!
    – addy
    Aug 1, 2014 at 1:11

1 Answer 1


Here is the documentation for createPortalUser. Additionally the Sites class documentation has the following statement:

If there is a exception when using site.createPortalUser, a null is returned and the site system administrator is sent an email. For more information on sites, see “Force.com Sites Overview” in the Salesforce online help.

If null is being returned, it is not being created. Perhaps you can look for an email with the site system administrator.

Some possible causes are:

  • Invalid data for the User, such as missing a required field, invalid length on a field, duplicate user name, etc.
  • Make sure that the account owner has a role in the role hierarchy. (I ran into this one before with a script. Nasty.). This might be easier to hit if you are using many different accounts and would explain why some fail and some do not.

More on the role hierarchy requirement:

It is that the portal user's contact's account Owner has to be in the role hierarchy. So, if you just have one generic account (e.g. static Id PORTAL_ACCOUNT_ID) that account must have an Owner that has a role. In your code, the cons[0].AccountId account needs to have an Owner in the role hierarchy.

The issue I ran into before was some Accounts in the org were owned by a user that the org specifically used to designate accounts that were not currently assigned to a sales rep and that user did not have a role. I had a script that created many portal users from contacts of many accounts and it failed on all of the contacts associated to that one account. So, if you look at the User record of the account Owner and it doesn't have a role in the hierarchy, that could be the problem.

User Edit with no Role

  • Thanks for the link Peter, I searched for this and could not find it! I am the site contact for the community and I have received no emails. I have added code to send me an email when it fails. And I don't see any reason why that contact/user self registration failed. my only thought is perhaps they have that username somewhere in SF world and it is failing....
    – addy
    Aug 1, 2014 at 1:36
  • @addy - That could be. All the User fields have to be valid (just as if entering through the UI) and duplicate user name isn't. There are a few other things I've run into. I updated the answer with more examples. Aug 1, 2014 at 1:49
  • Wow I never thought of the role hierarchy! I will take a look, thanks very much!
    – addy
    Aug 1, 2014 at 1:51
  • Hi Peter, can you explain the role hierarchy issue? The account owners are sales reps in their own hierarchy. The community portal user has no role (high volume).
    – addy
    Aug 1, 2014 at 1:56
  • Yes. I added more to the answer, because it was getting long in the comment. Aug 1, 2014 at 2:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .