+ Start a Discussion
AlokVAlokV 

Test class error

Hi,

I am writing a test class. Some code is below
@isTest
for (Integer i = 1; i < 5; i++) {
      Account a = new Account();
      a.Name    = 'Alok'+i;
      a.website = 'test@gmail.com'+i;
      a.Phone  = '9988712345'+i;
      insert a;
      accts.add(a);
    }
     Insert accts;
 
System.Debug(accts[0].Id);
System.Debug(accts[1].Id;

System.debug prints same account Id for for both the debug statements i.e. System.Debug(accts[0].Id) and System.Debug(accts[1].Id).
Why is this happening?  Am I making any mistakes?  

Any help would be appreciated.

Regards,
Alok
Brenda S FinnBrenda S Finn

Alok

The reason you are seeing the same account Id in both debug statements is because you are inserting the same Account two times, first inside of your loop:

Line 7: insert a;

And then again outside of your loop you add all the accounts in accts:

Line 10: Insert accts;

Remove either one of the insert statements and your test should run successfully.