function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Marc D BehrMarc D Behr 

Creating a person account with community access

I currently have a custom REST API call to create a new person account based on the info being passed after some error/configuration checks..We are now getting ready to implement a community and we want the new person accounts to also create and enable the user for a community login (at the same time that it is created).

In order to do this, I added code to my class to populate a new User and then I tried to call the Site.createPersonAccountPortalUser() call (which I assume will be able to replace the call that I currently have to create the person account), but the createPersonAccountPortalUser call is giving me the error "System.TypeException: That operation is only allowed from within an active site".

The code snippet  that I am working on is here:
User u = new User();
u.username = userID+'.wrsb1';
u.Email = userID;
User owner = [Select Id from User where UserRole.Name = 'Support Management' limit 1]; // pick an owner
Id x = Site.createPersonAccountPortalUser(u,,password);
The REST call is being made by an account that is also used to handle many other REST interface calls,  It is my understaning that Site.createPersonAccountPortalUser() only works from within a community, but I am not sure how to get a REST call to run under a community.

What am I doing wrong or is there some other way to create a person account and then enable them for community access programatically?