0

During the process of self Registration of users in a Experience Site, we need to create a new Account and a new Contact if the users email doesnt match any of the existing contacts.

However in order to be able to create user with a Portal Role the field IsCustomerPortal from the Account object must be true, if not, I receive an error when creating the UserRole (of PortalType = 'CustomerPortal') saying the Account is not enabled for Portal.

The problem is that if I try to set to true IsCustomerPortal during the creation of the Account I always get an error "Field is not writeable" (I even tried to update if after the insert and still get the error Update failed. First exception on row 0 with id XXX; first error: INVALID_FIELD_FOR_INSERT_UPDATE, Cannot explicitly enable account as Customer Portal)

Also I need to set the UserRoleId of the User object that System.UserManagement.initSelfRegistration takes as parameter , with a UserRole of type CustomerPortal and PortalAccountId = inserted Account Id. however, it doesnt allow me to insert the new role because the Account doesnt have the IsCustomerPortal field set to true, and if I try to pass the User object without UserRoleId then the insertion of the User fails due to "Portal Owner must have a role"

Is there any system method or any way to set this field to true programmatically in apex?

1
  • according to this old thread: developer.salesforce.com/forums/?id=906F00000008p7lIAA , the owner of the Account must have a role. Can you confirm that the user that is executing the self registration code or the owner of the account if you set it, has a role filled in?
    – Novarg
    Commented Mar 22, 2023 at 13:39

1 Answer 1

0

When you want to create a community user for a contact, just set the user ProfileId field to a community user profile. The system will automatically enable the account when you insert the user.

User theNewUser = new User(ProfileId = [YourCommunityUserProfile], ContactId = [TheContactId], etc ...)
2
  • The thing is that I already set the ProfileId to a Community Profile, the error comes due to the UserRole , I cannot create a UserRole of type CustomerPortal for the new Account because the account is not enabled yet as IsCustomerPortal. And if I leave UserRoleId null or with an Id of UserRole with no PortalAccountId it fails. Also note I am not inserting directly the user, nor creating it with Site.createPortalUser, but through and then when System.UserManagement.initSelfRegistration System.UserManagement.verifySelfRegistration is succesfully executed the method itself creates the user
    – Juan Ge
    Commented Mar 23, 2023 at 11:17
  • What is the license type? Community, Community Plus, Partner, etc? Please edit your post to include that info. Commented Mar 23, 2023 at 15:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .