You need to sign in to do that
Don't have an account?
Pratik Kumar
Please let me know what is the issue with the following code to insert a List<Account>
On executing the following code, an error message pops up saying, 'System.ListException: Before Insert or Update list must not have two identically equal elements'
String n;
Account tempac = new Account();
List<Account> ac = new List<Account>();
for (Integer i = 0; i < 10; i++){
n = 'TryAccount' + i;
tempac.Name = n;
ac.add(tempac);
System.debug('Account Contect is::: ' +ac[i]); //Checkpoint 1
}
//System.debug('This is the result:::: ' +ac); // Checkpoint 2
insert ac;
Checkpoint 1 outputs value as expected i.e. TryAccount0, TryAccount1 and so on
But
Checkpoint 2 gives all identical valuses as the output i.e. TryAccount9, TryAccount9, TryAccount9, and so on.
String n;
Account tempac = new Account();
List<Account> ac = new List<Account>();
for (Integer i = 0; i < 10; i++){
n = 'TryAccount' + i;
tempac.Name = n;
ac.add(tempac);
System.debug('Account Contect is::: ' +ac[i]); //Checkpoint 1
}
//System.debug('This is the result:::: ' +ac); // Checkpoint 2
insert ac;
Checkpoint 1 outputs value as expected i.e. TryAccount0, TryAccount1 and so on
But
Checkpoint 2 gives all identical valuses as the output i.e. TryAccount9, TryAccount9, TryAccount9, and so on.
Please try to update your code like below
Let us know if this will help you
All Answers
Please try to update your code like below
Let us know if this will help you