0

I have the community and logged in as a user with Customer Community user license and Customer Community User profile. There is VF page with the table that shows Contacts related to the logged in User`s Account record.

Logged in user should have the possibility to create new community users that will be assigned to the same Account record.

I am trying to create the new user with Site.createExternalUser(<user_instance>, <accoundId>) method.

It throws an Exception Site.ExternalUserCreateException: [You are already logged in.].

Is there any way on how to do that with Site class methods? I assume, that otherwise I can create Contact and then in @future method (coz of mixed DML) create the User with ContactId.

2
  • 1
    That future method will still run in the context of the logged in User who triggered the future call, so I believe it will still throw the same exception. developer.salesforce.com/forums/?id=906F00000009AiFIAU Dec 18, 2018 at 12:34
  • @PranayJaiswal, how could I create the user then? Expose the REST service and make a "self" callout to create the user?
    – m_konyk
    Dec 18, 2018 at 12:37

1 Answer 1

3

How about using Platform Events, Platform Events run in the context of the Automated Process and thus they can be used for such scenarios.

  1. Create an Event User_Creation_Event__e, with 2 fields, AccountID__C and UserJSON__C
  2. Create a Trigger on User_Creation_Event__e which will create Community users,
  3. From your COntroller , instead of calling Site.ExternalUserCreateException, just publish that event, by passing accountID and Serialized User JSOn .

Src: https://trailhead.salesforce.com/en/content/learn/modules/platform_events_basics

1
  • Not sure if it is still the only way, but just came to say this seems like a pretty slick solution.
    – errational
    Oct 6, 2023 at 20:57

You must log in to answer this question.

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