function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
34213421 

System.DmlException: 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; 
         
   
     }

The Controller code is 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;

Can someone help in this scenario​
Glyn Anderson 3Glyn Anderson 3
It's hard to help when you haven't provided the entire controller method.  Is that the "setTab" method?  Is the test class declared with "@seeAllData"?  Are you getting the error on line 47 of the test class?  Or line 5 of the controller?

I can't really tell if lines 4 & 5 of the controller are in the for-loop or not.
The if-condition will never be true though, as the size of a list can never be less than zero.

If you post a more complete example, someone might be able to help.
Raj VakatiRaj Vakati
Modify test class as like below 
@IsTest static void testRegFormWithNewUserAddress11() {
    
             
        Contact con = new Contact();
          con.Firstname = 'test';
          con.LastName ='test';
         con.OtherPhone = '8765432167';
         con.Organization_Type__c = 'test';
          con.Email ='testemail'+Math.round(Math.random()*Math.pow(10, 7))+'@testemail.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; 
         
   
     }

 
34213421
seeall data is not set in my code. I did not set it since the default is anyways zero. The error in the testclass is at line 28. The insert u and c are in the for loop.  
if(existalready.size()<0){

It is greater than 0. Sorry I copied incorrectly. I did remove the condition entirely, but still the issue is continuing. Not sure if I am missing something
34213421
Raj,

I did try that code too, but I still have the same error