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?