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
Oğuz Can AyverdiOğuz Can Ayverdi 

Community users can't create new users

We need certain users from our partner resellers to be able to create new users for their employees on our Partner Community. We have created a custom Lightning component for them to do this. This component works perfectly when used by a System Administrator. However, when it's used by a user with "Partner Community Login" license, it throws this exception:
Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Profile]: [Profile]
This is the Apex code in the controller that inserts the User:
@AuraEnabled
public static void createPartnerUser(Contact partnerContact, Id profileId) {
    
    [...]

    User currentUser = [SELECT Id, TimeZoneSidKey, LocaleSidKey, LanguageLocaleKey FROM User WHERE Id = :UserInfo.getUserId()];
    
    User newUser = new User(Username = partnerContact.Email, ContactId = partnerContact.Id, ProfileId = profileId,
            Alias = alias, Email = partnerContact.Email, EmailEncodingKey = 'UTF-8', Phone = partnerContact.Phone,
            LastName = partnerContact.LastName, CommunityNickname = partnerContact.Email.substringBefore('@'),
            TimeZoneSidKey = currentUser.TimeZoneSidKey, LocaleSidKey = currentUser.LocaleSidKey,
            LanguageLocaleKey = currentUser.LanguageLocaleKey, system_field_jump_future_method__c = true,
            FirstName = partnerContact.FirstName
    );
    insert newUser;
}
As you see, the Profile is not missing and we have confirmed that it is not null. The code works perfectly when it's executed by an Admin or as Anonymous Apex.

Thank you for your help.
Jesse RoweJesse Rowe
Customer and partner portal users can't access the profileId field.

https://developer.salesforce.com/docs/atlas.en-us.object_reference.meta/object_reference/sforce_api_objects_profile.htm