1

We have a force.com site where we use person accounts. I am building a registration page where i create a person account on the first step and then call the site.createpersonaccountportal user to enable the user for the account. I am getting a null on the return of this method and would like to know why this is happening? Here is the code below. Has anybody user person accounts and use the site.createpersonaccount user to enable the user please?

User u = new User();
u.Username = username;
u.Email = email;
u.CommunityNickname = communityNickname;

String perAcctRecTypeId= [select Id from RecordType where 
(Name='Person Account') and (SobjectType='Account')].Id;

try {
    personAcct = new Account();
    personAcct.RecordTypeId = perAcctRecTypeId; 
    personAcct.FirstName = username;
    personAcct.LastName = 'test site kumar';
    insert personAcct;
} catch (Exception e) {
    ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, 'Exception is'+e);
    ApexPages.addMessage(msg);
    return null;
}

accountId = personAcct.Id;
Id portalId = Site.createPersonAccountPortalUser(u,accountId,password);

I am getting a null on the portalId variable here.

2

We use that method on our site. I had to set a LOT more user fields, and also had to set a system administrator as the account owner.

Also, add a pagemessages to your registration form...it may help you identify what salesforce doesn't like about your registration.

Notes about the code below:

  • we run this as a remote action from a visualforce page
  • That TrueCostUtilities.lookuptheadmin() just returns the ID of our systemadmin, who becomes the new account owner. Store in a custom setting to make life easy.
  • Duplicate nickname problems were the biggest headache--we don't want our users dealing with nicknames, so if someone already has the default for their name, we just keep incrementing integers on the end until we're sure it's unique.
  • There's probably a better way to handle that to avoid "query in a loop" but it works fine so far. Extremely common names will just be slower to register, and if you expect hundreds of people with the same name, it'll be a prob. If I were building it today, I'd append a datestamp as the number and then start checking from there.

        global static string Register(string first, string last, string email, string password){
            //check for existing user by email
            list<user> ulist = [select id from user where username =: email];
            if (ulist.size()!=0) {return 'That email address has already been used.';}
            integer x=1;
            string trynickname = first+last;
            ulist = [select id from user where communitynickname =: trynickname];
            if (ulist.size()>0){
                do{
                    x++;
                    trynickname = first+last+x;
                    ulist = [select id from user where communitynickname =: trynickname];
                } while (ulist.size()>0);
            }
            user u = new user();
            u.communitynickname = trynickname.substring(0,math.min(trynickname.length(), 38));
            u.firstname=first;
            u.lastname=last;
            u.email=email;
            u.username=email;
    
    
            id userId;
    
            try{
                userId = Site.createPersonAccountPortalUser(u, string.valueof(TrueCostUtilities.lookuptheadmin()), password); 
            }catch (exception e){return 'There was an error with your registration.'; } 
    
    
            return 'Success';
        }
    
  • Can you please share your code @Shane McLaughlin – sunny Mar 11 '14 at 13:24
  • yes, along with some explanations. – Shane McLaughlin Mar 14 '14 at 13:26
0

If I recall, Site.createPersonAccountPortalUser and Site.createPortalUser must run as the Site Guest User. It will only work for an anonymous user accessing the public-facing Site URL.

Try the following:

  1. go to Setup > Debug Logs,

  2. monitor your Site Guest User,

  3. removing the exception around your code,

  4. triple-check you are running the code from the public-facing Site URL

then look at the log entry.

  • Hi User320, I did your steps and i can see it is running out of my public facing url only. But still i am getting a portaluserid as null? Any other inputs would be greatly appreciated.. – buyan kumar Apr 22 '13 at 18:17
  • @Shane McLaughlin Could you please post the source code which you use please? This would help me asap.. – buyan kumar Apr 23 '13 at 3:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.