How to i Create Person Account Portal User in Test class. I am using Site.createPersonAccountPortalUser(u, ownerId, password); to create User.
[SalesForce] How to Create Person Account Portal User in Test class
Related Solutions
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.
To Enable person Account is salesforce ,first and foremost thing is to contact salesforce support and get it enabled
The next step will be as per implementation guide
Best Answer
You have to first create the Person Account (as a User with assigned Role) and then use the generated PersonContactId field on Person Account for User.ContactId ... something like this: