4

Apex testmethod to create High Volume Portal Users

// mock User(s) to create SObjects
...
// insert Account(s) and Contact(s)
...
system.runAs(someUser) {
  insert new List<User> {   new User (
                                        Alias               = '_' + 'user00'+'_',
                                        ContactId           = someContactid,                        
                                        Email               = '_user00@bar.com',
                                        EmailEncodingKey    = 'UTF-8',
                                        FirstName           = '_fname',
                                        IsActive            = true,
                                        LanguageLocaleKey   = 'en_US',
                                        LastName            = '_' + 'user00' + '_lname',
                                        LocaleSidKey        = 'en_US',
                                        ProfileId           = someProfileIdthatIsNonNull,
                                        UserName            = '_' + 'user00' + '_foo@bar.com',
                                        TimeZoneSidKey      = 'America/Los_Angeles'
                                                ) };
  • someContactId is valid, was mocked earlier
  • The Account(s) were created with a user that has a userRoleId != null
  • someProfileIdThatIsNonNull is a valid ProfileId for a Community Community License

Yet, this error ensues on insert.....

REQUIRED_FIELD_MISSING, Required fields are missing: [ProfileId]: [ProfileId]
5

(This cost me an hour of time to figure out, posting to save others that time)

First of all, the answer is not isTest (seeAllData=true)

Here's the solution ...

Even though the User.ProfileId value is non-null and valid (proven through debug statements), if the running user doesn't have permissions to create Users, you get this error. Had I gotten an error indicating that the running user didn't have CRUD permissions on User this wouldn't have merited a question or answer. But the required fields error is detected first before the insert and hence the confusion.

An easy trap to fall into when your mind is focused on the special nuances around HVP users and writing testmethods to verify your custom community logic.

Basically, I was trying to mock data where the someUser represented the Community Manager but I didn't mock that user correctly.

Creating SObjects and setup data in a testmethod frequently runs into MIXED_DML error wherein you need to use System.runAs(...) around your SObject creation before the Test.startTest(). Hence, be sure your running User has permissions to create the necessary Sobjects.

  • Wait, your someUser isn't someAdmin? I always insert User records as System Administrator. – Adrian Larson Feb 12 '17 at 0:06
  • 1
    @AdrianLarson - as I said, the use case was simulating a Community Manager who had delegated admin privileges and I forgot about that in the mocking. So, yes, face plant --your mind goes into thinking your profileId is wrong or there is something unique about Communities or the contactId isn't set up right or anything but the now, obvious. – cropredy Feb 12 '17 at 0:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.