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;