1

I have one custom object which I wanted to create 3 records from 1 record. Basically the requirement is, I have a field amount (ex as 100 $) which should be divided into 3 different records of amount 30$, amount 35$ and amount 35$. So I am querying the records and trying to loop it and creating 3 different records but it throws error as

System.ListException: Before Insert or Upsert list must not have two identically equal elements

There are more than 100 fields in the object.

Below is my code

List<customobjects__c> amList = [SELECT id, Name, amount__c... (all fields of object) FROM customobjects__c];
List<customobjects__c> amNewList = new List<customobjects__c>();
for(customobjects__c c : amList){
    customobjects__c c1 = new customobjects__c();
    c1 = c;
    c1.amount__c = c.amount * 0.30;
    amNewList.add(c1);

    customobjects__c c2 = new customobjects__c();
    c2 = c;
    c2.amount__c = c.amount * 0.30;
    amNewList.add(c2);

    customobjects__c c3 = new customobjects__c();
    c3 = c;
    c3.amount__c = c.amount * 0.30;
    amNewList.add(c3);
}
insert amNewList;

I understood the error that while inserting it thinks we are trying to add duplicate rows but it is not duplicate rows. It would be very helpful for me if somebody can help me. If anything is required from my side please let me know.

0
2

This error refers to "references", not literally having the same data. As it stands, c1, c2, and c3 all reference the same object, c:

for(Customobjects__c c: amList) { // example: c is 0x1234
  customobjects__c c1 = new customobjects__c(); // c1 is 0x2345
  c1 = c; // c1 is now 0x1234
  ...
  c2 = c; // c2 is now 0x1234
  ...

Oops. You are actually trying to use the same reference multiple times; your second assignment clobbers the first assignment, so the "new" objects disappear.

You'll want to instead clone the object:

for(Customobjects__c c: amList) { // example: c is 0x1234
  // clear Id, timestamps, and autonumber fields
  CustomObjects__c c1 = c.clone(false, true, false, false); 
  c1.amount__c *= 0.3;
  ...
2
  • It says <INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]> should I specify c.id = null; Dec 13 '17 at 6:42
  • @Sagar make sure the first parameter to clone is false. Double-check your code.
    – sfdcfox
    Dec 13 '17 at 6:46
0

This error is occurring because you are actually inserting identical elements in the list. Let's go through you code: Suppose the list amList returns three records. You are then iterating on all the records. Now in the iteration, for the first record you create a record with amount as c.amount * 0.30. So far so good? Now for the second record c2, this record has the same values of the above created record c1, with c.amount * 0.30 exactly same as the above record. I believe this is why you are receiving the error which says you have identical elements in the list.

1
  • ok 0.30 and all was just an example.. actual values are 0.31, 0.57 and 0.12.. Dec 13 '17 at 6:41

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.