What is the best practice of creating community users? Is it by using Site.createExternalUser api or directly inserting user object?
1 Answer
Creating a user and a contact in the same transaction is not allowed. You end up with a MIXED_DML
exception. Workarounds exist, but are not ideal. If you use Site.createExternalUser
, it will create the user and contact (if necessary) all in one step. In addition, you can also set their default password (or any supplied password) at once, which can also be used with Site.validatePassword
to make sure the passwords match and match the org's password policies. In other words, you should not be using a standard DML operation to create external community users. The Site method is the recommended approach.
-
So what if I want to relate the community user to an existing contact. And the matching contact is identified using a custom text field . In that scenario whether i can use Site.CreateExternalUser? Sep 22 at 6:48
-
@LoneWolf777 The createExternalUser method matches strictly by email, not a custom field. If you already have the custom data you're looking for, it should be easy to get the email for the contact from there.– sfdcfoxSep 22 at 14:31
Site.createExternalUser
has a myriad of issues, it's only good in very few happy path scenarios.