[SalesForce] Insert failed. First exception on row 0; first error: PORTAL_USER_ALREADY_EXISTS_FOR_CONTACT, portal user already exists for contact: []

I am trying to insert portal user records and receiving this error

System.DmlException: Insert failed. First exception on row 0; first error: PORTAL_USER_ALREADY_EXISTS_FOR_CONTACT, portal user already exists for contact: []

Here is the code

@IsTest static void testRegFormWithNewUserAddress11() {

        Contact con = new Contact();

          con.Firstname = 'test';

          con.LastName ='test';

         con.OtherPhone = '8765432167';

         con.Organization_Type__c = 'test';

          con.Email ='Sitetestg466@hotmail.com';

        insert con;

             Order__c ordr = new Order__c();

          ordr.Contact__c = con.Id;

          ordr.Account__c = a.Id;

          insert ordr;

       Order_Line__c ordrline = new Order_Line__c();
     ordrline.Order__c = ordr.id;

          insert ordrline;


    ControllerTest controller = new ControllerTest();       

controller.con = con;

        controller.acc = a;

        controller.Password= 'testpass123';

        controller.checkIfContactExists();

        controller.checkIfAccountExists();

         controller.setTab();

             Profile p = [select Name, id From Profile where UserLicense.Name like '%Customer Community%' limit 1];

           User u = new User ();

          u.FirstName = 'test';
          u.LastName ='test';

          u.Email ='testemail'+Math.round(Math.random()*Math.pow(10, 7))+'@testemail.com';

          u.Username = 'testemail'+Math.round(Math.random()*Math.pow(10, 8))+'@testemai.com';
          u.Alias ='test';
          u.CommunityNickname ='tst';
          u.ContactId =con.Id;
          u.LanguageLocaleKey = 'en_US';
                    u.LocaleSidKey ='en_CA';
                    u.TimeZoneSidKey ='America/Chicago';

                    u.ProfileId = p.Id;

                    u.EmailEncodingKey = 'UTF-8';
                 insert u;

     }

Controller code below

List<User> existalready = [select id,Contact.FirstName,Username,Contact.LastName from user where username =:Email and IsActive = true LIMIT 1];
                if(existalready.size()>0){

                    for(User u: existalready){
insert u ;
insert con;

Best Answer

Your logic isn't functional, so you aren't effectively checking for existing records.

if(existalready.size()<0){

The size() of a List will never be less than zero. Based on this code, it is not clear how you are inserting records at all, since the if branch should never execute. If it did execute, you'd be inserting objects that already exist, which you just queried from the database.

It's not clear what, if anything, this actually does. You need to look carefully at your logic, and I would recommend reevaluating your unit test in detail and adding appropriate assertions to make sure your code does what you think it does.

Your edit changing < to > does not fix the code. Your code is still inserting records that it just queried, and performing DML in a loop.