1
Account acc = new account(name='Rahul');
insert acc;
account acc1 = acc.clone();
insert acc1;
Account accc = [select id from account where id =: acc1.id];
system.debug(accc.isClone());//Supposed to display true

But it is being displayed as False. It is supposed to show true as acc1(accc) is a clone of acc.

1
  • 2
    The isClone() method will only return true for SObject that are clones in memory. So because accc is an actual record, loaded from the database, isClone() will return false. If you need this functionality, you will have to keep track of the fact that a record is cloned using a custom field and, for instance, a workflow (see help.salesforce.com/…) Jan 9, 2021 at 9:29

1 Answer 1

2

The purpose of isClone is to validate records in memory, not in the database.

Account acc = new Account(Name = 'Acme');
insert acc;

Account clonedInMemoryAccount = acc.clone();

// Test before committing to DB
System.assertEquals(true, clonedInMemoryAccount.isClone());

insert clonedInMemoryAccount;

Account databaseAccount = [
    select id
    from Account
    where id = :clonedInMemoryAccount.Id];

System.assertEquals(false, databaseAccount.isClone());

In this scenario you can see the difference in the two assertions and how you should expect to use isClone from the SObject class.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .