i have developed one visualforce page and it has multiple Group with Product and Bundle so, once user insert multiple Group with the Same product and same bundle then it's giving me an error as following

Duplicate id in list

What should i need to modify in the following method

public void Save(){
    if (Quote.Name == '' || Quote.Name == Null){
        Quote.Name.addError('Please Enter Quote Name');
        return null;
    upsert Quote;

    QBP.Quote__c = this.Quote.Id;
    upsert QBP;

    Product_Quantity__c SavePQ = new Product_Quantity__c();
    List<Product_Quantity__c> SavePQList = new List<Product_Quantity__c>();
    Bundle__c bn = new Bundle__c();
    List<Bundle__c> bndlList = new List<Bundle__c>();
    List<Group__c> GroupList = new List<Group__c>();

    if (grpWrappList != null || grpWrappList.size()>0 ){ 
        for(GroupWrapp GW : grpWrappList){
            GW.WrappGRP.Quote_Bundle_Product__c = QBP.Id;
            upsert GroupList;

            for(ProductWrapper PW : GW.ListProductWrapper){
                SavePQ = PW.TempProductSelection;
                SavePQ.Group__c = GW.WrappGRP.Id;

            for(BundleWrapper BW : GW.ListBundleWrapper){
                bn = [select Id from Bundle__c where Id =: BW.TempBundleselection.Bundle__c];
                bn.Quote_Bundle_Product__c = QBP.Id;
                bn.Group__c = GW.WrappGRP.Id;
            Upsert bndlList;

    for(Product_Quantity__c P : SavePQList){
        P.Quote_Bundle_Product__c = QBP.Id;

    If(SavePQList.size() > 0){
        upsert SavePQList;
  • Idea is to save your data into map by key, so you can do an update it by values. But in that case you can not user upsert. F.E. instead of upsert do for(Product_Quantity__c p : SavePQList) { if (p.Id != null) {some_map.put(p.Id, p);} else { some_list.add(p);} } insert some_list; update some_map.values(); But be sure that duplicates are not overriding important data. Ideally, there should be no duplicates in those situations. P.S. DML and SOQL may cause errors in loop. – kurunve Mar 3 '17 at 10:19

You should consider using the Map class instead of a List.

In fact, if you instantiate some maps like this:

Map<Id, Bundle__c> bndlList = new Map<Id, Bundle__c>();

The filter on duplicated ids will be automatically done. It won't allow you do add two times the same Bundle__c object.

To add an object to this map use: bndlList.put(bn.Id, bn);

Then, to update all the objects added to the map, use:

update bndlList.values();
  • Can i use upsert instead update ? – WantToBeACoder Mar 3 '17 at 10:32
  • There's no need for an upsert on this list. You never instantiate an object. – Martin Lezer Mar 3 '17 at 10:35
  • Yes. correct, i tried it with both case and it's working and error is no more alive so, need to know only one thing : i am trying to insert two same Bundle in different Group (Like: G1 has Bundle1 & G2 has Bundle1) in SF database it's only inserting the Bundle1 in G2 not in G1...any specific reason ? – WantToBeACoder Mar 3 '17 at 10:41
  • I'm sorry I don't understand the question. Can you post an another question about it with sample code? – Martin Lezer Mar 3 '17 at 12:33
  • thanks for the reply :) here is the latest Post – WantToBeACoder Mar 3 '17 at 12:34

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.