We are rolling out a community for our customer base and I am attempting to create an Batch Class to create and activate OR deactivate Community Users based on when they have been deemed to be active by a Designated user from their organization. Our Account-Customer structure is not standard - Most of our Contacts do not role up to an Account.

I am running into an issue when attempting to insert my new Users because I don't have specific fields related to Role populated. When we create an External User via the Salesforce UI, the role is automatically created as " Customer User". However, when I am creating them through the Apex Batch, I don't do so.

Must I create the UserRole for the Account prior to inserting the User? Is there something that I am missing where Salesforce should do this on my behalf?

Two things would be helpful to me:

  1. A list of the minimum fields that must be present to create a new Community User from a Contact that has an Account.
  2. a Blog post or Salesforce Documentation that outlines what happens on the backend when creating a Community User.

Or is there a really easy built in Class/Method that I am unaware of that would solve this problem for me and make me feel really dumb for not having found it yet?

2 Answers 2


I think you're looking in the wrong spot. The role that needs to exist is on the owner of the account record, not directly on the community user.

We have a workflow that forces all of our contacts/accounts to have the same user act as the owner and gave that owner a very generic role which allows us to easily create community users via apex.

I can understand that won't work for everyone...

Make sure the Account's Owner has a role:

if (contact.Account.Owner.Role != null) {
    // Do your thang

Minimum Fields:

We have the following fields defined in our JIT handler:

            u.firstName = '';
            u.lastName = '';
            u.ContactId = contactLookup[0].Id;
            u.Username = 'username';
            u.Email = 'email';
            u.CommunityNickname = 'nickname';
            u.Alias = '';
            u.TimeZoneSidKey = 'America/Phoenix'; // Required
            u.LocaleSidKey = 'en_US'; // Required
            u.EmailEncodingKey = 'ISO-8859-1'; // Required
            u.LanguageLocaleKey = 'en_US'; // Required
            u.ProfileId = '<community-profile-id>';
  • Thanks!! My test class was screwed up because I was assigning Contact.Account = acct rather than Contact.AccountId = acct.Id. It wasn't throwing an assignment error, but it was also never assigning an Account to the Contact. Your code pointed me in the correct direction. Big help. Thanks.
    – RD3
    Commented Apr 3, 2017 at 13:18

This is a useful guide to the options around user provisioning for communities https://developer.salesforce.com/blogs/developer-relations/2014/06/how-to-provision-salesforce-communities-users.html

You may also find the trailhead on external identity (Identity for customers) useful: https://trailhead.salesforce.com/modules/identity_external

You must log in to answer this question.

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