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 = '[email protected]';
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"/>
PortalOwnerId
? Are you able to manually create a portal user with that owner?