• Oğuz Can Ayverdi
  • NEWBIE
  • 0 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies
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.