I have created a community where any public user can self register.

As a developer, I'm developing a different functionality for Self registration Page.

Now I've to test self Registration functionality. Salesforce not allow to create a user with duplicate email. and we are not able to delete the user. So here How can i fully test this functionality.

I have already created 15-20 User until with different email.Now how can I move forward to test this?

  • To create a User, User Name needs to be unique not Email. So you can create multiple users with same email but different User Names.
    – Venky
    Aug 9, 2017 at 10:35
  • @AjayPrakashDubey Even I deactivate it does not allow me to create user with same email. In my code I've setup the username of customer is email.
    – Amol
    Aug 9, 2017 at 10:45
  • @Venky I've setup the username of customer is email
    – Amol
    Aug 9, 2017 at 10:46
  • then you need to make Email of customer unique field.
    – Venky
    Aug 9, 2017 at 11:24

1 Answer 1


Create user inside test class and check for functionality

private class YourTestClass {

      private static Integer genRandomNumber() {
        return Crypto.getRandomInteger();
      private  User genPersonAccountUser() {
            User testUser = new User();
            testUser.Username = 'Name+' + genRandomNumber() + '@example.com';
            testUser.Lastname = 'LastName';
            testUser.Firstname = 'FirstName';
            testUser.Alias = ('ABCD' + genRandomNumber()).left(8);
            testUser.email = 'Name+' + genRandomNumber() + '@example.com';
            insert testUser;
            return testUser;
     private static void testForUser() {
         List<user> users=new List<user>();
         for(int i=0;i<10;i++)
          //Test Functionality using users 

In Org you can create user with same Email address but not Same username

  • In my test class also I'm not able to create a community user. "userId = Site.createExternalUser(u, accountId, putPassowrd);" This part is not creating user.
    – Amol
    Aug 9, 2017 at 10:57
  • To allow an external user to access your community, add them to an account as a contact record and then enable the contact record as a customer user or partner user, depending on your business relationship.
    Aug 9, 2017 at 11:04
  • In test Case also you have to create contact and account to an user , then try to create External user
    Aug 9, 2017 at 11:06

You must log in to answer this question.

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