Can’t create two different new Account objects in Salesforce test

I’m getting some strange behavior when I’m setting up some data for my test. I’m creating two account objects, but when I add a checkpoint and look at the account objects, they have the same data.

My account setup inside the test does this:

Account account1 = createAccount();
account1.Name = 'Account 1';
insert account1;

Account account2 = createAccount();
account2.Name = 'Account 2';
insert account2;

My method that creates an account looks like this:

static Account createAccount() {
  Account account = new Account();
  account.Name = 'New account';
  account.BillingStreet = 'street';

  return account;
}

The checkpoint looks like this after the test runs. Notice the id and name are the same for both variables:
both objects have the same id and name

I have put in some more checkpoints and the two objects start having the same values as soon as I create the second account. It doesn’t make a difference whether I insert account1 before I create account2 or not.

Any idea what I’m doing wrong?

Answer

I tried with simpler version of your code and it appears as bug in developer console.

public class try1{
public try1(){
    Account account1 = new Account();
    account1.Name = 'Account 1';    

    Account account3 =  new Account();
    account3.Name = 'Account 3';   

    System.debug('##' + account1.Name);
    System.debug('##' + account3.Name);
}

}

When i check with checkpoints on line 10 & 11 it shows both account names as Account 3.

Attribution
Source : Link , Question Author : Helephant , Answer Author : AtulRajguru9

Leave a Comment