We have test classes that are failing when we try to insert a portal user. Error: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, To create or update users for this profile, go to Setup > Communities Settings and select Allow using standard external profiles for self-registration and user creation.: [ProfileId]

Is there a way to create a portal user in the test class without enabling this feature. As a best practice Salesforce recommends not to turn on this feature. ("allow using standard external profiles for self registration and user creation") we dont want to either just for fixing test classes.

  • did you activate the "Prevent Using Standard External Profiles for Self-Registration and User Creation" critical update?
    – sdandamud1
    Jan 28, 2020 at 19:59
  • This is by default enabled by Salesforce in winter 20 if you org is using standard external profile.They gave us an option to disable that feature but its highly recommended by salesforce not to disable it unless there is a strong reason.
    – venky
    Jan 28, 2020 at 20:38
  • have you found resolution on this?
    – sdandamud1
    Jan 29, 2020 at 22:02
  • Hey there, this just came up in my org today too. Jan 30, 2020 at 0:13
  • as per salesforce we have to enable it "allow users to self register" for test class to create portal user from standard profile, Alternate solution is to create portal user using custom profile. (runas as user with custom profile)
    – venky
    Feb 1, 2020 at 17:20

1 Answer 1


So I've found a tentative solution; the error seems to go away when you create a user with a custom profile. If you can, try updating your test class with a non-standard profile.

  • Thanks It worked but we have to run some classes as system admin which is a standard profile. To fix the issues we enabled "allow users to self register" for now
    – venky
    Feb 1, 2020 at 17:22
  • I found that "System Admin" didn't throw this error in my case, despite being a standard profile. Odd behavior but I'll take it. Feb 4, 2020 at 19:40
  • Interesting. I tried it but got the same error message in full copy sandbox but it worked fine in production.
    – venky
    Feb 11, 2020 at 17:29
  • The System Admin work-around no longer seems to work: 17:22:45:829 FATAL_ERROR System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, only portal users can be associated to a contact: [] May 4, 2020 at 21:24
  • I don't think we can create a custom profile within Apex code: salesforce.stackexchange.com/questions/5117/… May 4, 2020 at 21:28

You must log in to answer this question.

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