Find your content:

Search form

You are here

How should I use Site.createPersonAccountPortalUser to create a Person Account and activate them on the portal in one step?

 
Share

I have a method below which is supposed to be called from an unauthenticated Sites page (via https). It is intended to take some personal details and create a Person Account with them and then activate that record for use on the customer portal.

However when I run this method I find that the return value of the Site.createPersonAccountPortalUser method is null and the person account is not created.

I have been trying to follow the instructions from the Site Class documentation without success.

I have set up a force.com site, associated it to a customer portal, enabled the profile and defined a default user profile and licence to be high-volume portal user.

public static String createPersonAccountPortalUser(String FirstName, String LastName, String Email, String Phone, Map<String, String> mResponseMap){
    ID userId;
    User u = new User();

    //Name and email are required.
    if(FirstName != null && FirstName != '' && LastName != null && LastName != ''){
        if(Email != null && Email != ''){
            u.FirstName =FirstName;
            u.LastName =LastName;
            u.Username = Email;
            u.Email = Email;
            u.Phone = Phone;
            u.CommunityNickname = LastName;             
            system.debug('## createPersonAccountPortalUser: user =' + u);
            system.debug('## createPersonAccountPortalUser: PortalOwnerId = ' + PortalOwnerId);
            String strRecTypeId = queryForPersonRecType();
            try{
                userId = Site.createPersonAccountPortalUser(u, PortalOwnerId, 'testpassword');
                system.debug('## createPersonAccountPortalUser: userId = ' + userId);
                mResponseMap.put(KeyId, userId);
                UtilityMap.appendValue(KeyStatus, SuccessNewPersonAccount, mResponseMap);
                return userId;                                      
            }catch(Exception e){
                UtilityMap.appendValue(KeyError, FailureNewPersonAccount, mResponseMap);
                mResponseMap.put(KeyErrorMessage, EncodingUtil.urlEncode(e.getMessage(), 'UTF-8'));
                return null;
            }                               
        }else{UtilityMap.appendValue(KeyError, EmailErr, mResponseMap);}            
    }else{UtilityMap.appendValue(KeyError, NamesRequired, mResponseMap);}
    return userId;      
}

I have also tried running the following code as anonymous apex in the developer console and I still get a null returned.


User u = new User();
u.FirstName ='FirstName';
u.LastName ='LastName';
u.Username = 'testuser';
u.Email = 'testuser@magardner.co.uk';
u.Phone = 'Phone';
u.CommunityNickname = 'LastName';
String userId = Site.createPersonAccountPortalUser(u, Label.B2C_Portal_Person_Account_Owner_Id, 'testpass');
system.debug('user Id = ' + userId);

What am I doing wrong?

For reference here is my visualforce page

<apex:page showHeader="false" sidebar="false" controller="ServiceRegistrationController" action="{!createRegistration}" contentType="application/json">
<apex:outputText value="{!response}" escape="false"/>


Attribution to: Born2BeMild

Possible Suggestion/Solution #1

I found some further information here: Provisioning Customer Portal Users with Apex

This post suggests that the problem is that you cannot create a contact and a user record in the same APEX transaction. Doing so is called mixed DML and is not allowed. Therefore if you must create a contact and user at the same time the only way to do so is for one of them to be created asynchronously via a method marked with the @future annotation.

This is the method I have used.


    //Async method, using @future, to create the User record and associate it to the previously created Contact
    //This uses @future because you can not have mixed DML operations for standard objects (Account, Contact) and Setup objects(User)
    @future public static void createUser(String contactId, String email, String firstName, String lastName, String userName, String profileId) {
        Database.DMLOptions dmo = new Database.DMLOptions();
        dmo.EmailHeader.triggerUserEmail = true;
        //take the first few characters of the username for the alias
        String alias = userName.substring(0, 7);
        User u = new User(alias = alias, email=email, 
            emailencodingkey=EmailEncoding, lastname=lastname, languagelocalekey=LanguageLocaleKey, 
            localesidkey=LocaleSidKey, profileid = profileId, contactId=contactId,
            timezonesidkey=TimeZoneSidKey, username=username);

        u.setOptions(dmo);
        insert u;
    }

This appears to be working better than my previous attempt. The error messages now show up in the Apex Jobs page (Administration Setup > Monitoring > Apex Jobs) which makes it easier to debug than the silence that the first attempt gave.


Attribution to: Born2BeMild
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/2083

My Block Status

My Block Content