5

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?

  • 1
    You could try adding the following assert in multiple places to see where the account1 reference ends up pointing at account2. Assert Statement: System.assertEquals('Account 1', [select Name from Account where Id = :account1.Id].Name); – BarCotter May 21 '14 at 11:06
  • 1
    I can not reproduce this issue using your code. Is there any other code that you have left out that may be important? ie a static variable for Account? – BarCotter May 21 '14 at 11:25
  • Thanks @BarCotter, putting together a simple sample helped me figure out what was going on. – Helephant May 21 '14 at 12:14
0

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.

| improve this answer | |
  • It seems to be only an issue with checkpoints. When I use a system.debug() command, I get the right Id and Name for each object. – Helephant May 21 '14 at 12:13
1

This looks like an issue with the checkpoint rather than your code. I added the test below. The assertNotEquals line in the code is passing, meaning that the account1 and account2 have different id values.

@isTest
private class SimpleTest {

    @isTest
    static void test() {
        Account account1 = createAccount();
        account1.Name = 'Account 1';
        insert account1;

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

        System.assertNotEquals(account1.Id, account2.Id);
        System.debug('Account1: ' + account1.Id + ' Account2: ' + account2.Id);
    }

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

      return account;
    }
}

But when I add a checkpoint to the last line it is showing that both accounts hold a reference to account1, see screenshot below.

The debug statement is printed out is Account1: 0015000000t4jQFAAY Account2: 0015000000t4jQGAAY, which verifies that the code is correct.

enter image description here

The next steps for you on this would be to create a case with SalesForce to let them know that the checkpoints are not working correctly.

| improve this answer | |
  • I'll do that. Thankyou for your help, but I'd already marked the other answer as correct before I saw yours. – Helephant May 21 '14 at 12:15
  • No problem @Helephant. – BarCotter May 21 '14 at 12:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.