2

Im getting above error when I try to add some custom records to a list and insert it later. Here's what Im doing. From the following code, I call a method from a different class (SetupBase) and add the records

public static List<object__c> SOsToCreate = new List<object__c>();

SOsToCreate.add(SetupBase.createSOList('Test1', Buyer1.Id, Acc1.Id, PLTOrg1.Id, '[Test]', 'Stage', 'Type', FALSE, NULL));
SOsToCreate.add(SetupBase.createSOList('Test2', Buyer1.Id, Acc1.Id, PLTOrg1.Id, '[Test]', 'Stage1', 'Type1', FALSE, NULL));
SOsToCreate.add(SetupBase.createSOList('Test3', Buyer2.Id, Acc2.Id, PLTOrg2.Id, '[Test]', 'Stage2', 'Type2', FALSE, NULL));

insert SOsToCreate;

Here is the method on SetupBase class

public static object__c createSOList(String Name, Id BuyerId, Id TPId, Id PLTId, String ADName, String Stage, String Type, Boolean isVal, String Act){   
object__c SO1;    
SO1 = new object__c(Name = Name, Buyer__c = BuyerId, partner__c = TPId, Org__c = PLTId, Type__c = 'Value', Doc__c = ADName, Stage__c = Stage, Customer_Type__c = Type, IS_Val__c = isVal, Activation__c =Act, req_doc_gtn_member_agr__c=true, req_doc_bank_acc_veri__c=true);

return SO1;
} 

Any idea on what goes wrong here??

Many Thanks, Lahiru

1
  • because in your list createSOList two items are same. Put some check and debug the list and then remove the duplication. Or use Set to remove the duplication. Commented May 13, 2016 at 13:59

2 Answers 2

1

If you run this in "Execute Anonymous" it will work:

Contact c1 = new Contact(LastName = 'Smith');
Contact c2 = new Contact(LastName = 'Smith');
insert new Contact[] {c1, c2};

but if instead you replace the last line with:

insert new Contact[] {c1, c1};

the error you report will result.

So the root cause is adding the same object reference more than once into the list that you insert.

There is nothing in the code you have posted that would cause this error so I presume the problem is in some code you have not posted. The best way to solve this is to review the code and make sure the same object reference is not added more than once into the list. Using sets (that detect objects with the same field values) only solves the problem as a side effect and might eliminate duplicate value objects that should not be eliminated.

1
  • Hey thanks for point me to the correct direction. Yes it was NOT coming from the mentioned code snippets. But from another code which leads to create similar records from similar object. Commented May 13, 2016 at 17:09
1

Create a new Set and use AddAll() from your List to remove any duplicate records. Then, either add the results to a new List for insert/upsert or else clear the original List before doing the AddAll() from the Set back to the original List. That will remove the duplicate records. You need to do the latter because you can't insert or upsert from a Set. With most methods, you need a List instead.

1
  • Many Thanks for the update. This is a test class by the way. So I want to insert different records with different Stages for the code coverage. So im trying to figure out the root cause here? Commented May 13, 2016 at 14:14

You must log in to answer this question.

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