function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
KMK91KMK91 

two identically equal elements ​​​​​​​

Hi Team,
In debug i found it's inserting duplicate values how to avoid duplicate values using apex class.

Error:

Before Insert or Upsert list must not have two identically equal elements

Thanks
KMK
Best Answer chosen by KMK91
mukesh guptamukesh gupta
Hi,

Please follow below code:-
 
trigger ContractinsertandUpdate on Contract (before insert, before update,after insert, after update) {

    Map< Id , Id > OppIdToContractId = new Map< Id , Id >();
    List<OpportunityLineItem> OpportunityLineItemList = new List<OpportunityLineItem>();
    Set<ID> oppIds = new Set<ID>();
    List<Contract_Items__c> OldCIs = new List<Contract_Items__c>();
    Map<string,Contract_Items__c> mapOliItems =new  Map<string,Contract_Items__c>();

    if(Trigger.isAfter)
    {
  
        for(Contract contract :trigger.new){
            
            OppIdToContractId .put(contract .Opportunity__c,contract .Id);
            //oppIds.add(contract .Opportunity__c);  
        }

        OpportunityLineItemList = [select Id,Name,Product2.Name,Product2.Id,Product2.ProductCode, Quantity,UnitPrice,
                   CurrencyIsoCode,OpportunityId from opportunityLineItem
                   where OpportunityId IN :OppIdToContractId.keySet() ];
        
        
        OldCIs = [select Id, name,Item__c from Contract_Items__c where contract__c IN :trigger.new ];
        
        for(Contract_Items__c oItems :OldCIs ){
          
            mapOliItems .put(oItems .Item__c ,oItem);
        }
              if(OldCIs .size() == 0){
            if(OpportunityLineItemList .size()>0){
			    list<Contract_Items__c> newItems = new List<Contract_Items__c>();

                for(opportunityLineItem oli :OpportunityLineItemList ){
                    Contract_Items__c CI = new Contract_Items__c();
                    CI .Name = oli.Name;
                    CI .Offering__c = oli.Product2.Id;
                    CI .Contract__c = OppIdToContractId .get(oli.OpportunityId);
                    CI .Quantity__c = oli.Quantity;
                    CI .Price__c= oli.UnitPrice;
                    CI .Item__c = oli.Product2.ProductCode;
                    newItems .add(CI ); 
                }
				if(newItems.size() > 0)
					insert newItems ;
            }
        }else{
			list<Contract_Items__c> existingItems = new List<Contract_Items__c>();

            for(opportunityLineItem oli22 :OpportunityLineItemList ){
              
                if(mapOliItems .containsKey(oli22.Product2.ProductCode)){
                    Contract_Items__c newCI1 = mapOliItems .get(oli22.Product2.ProductCode);
                    newCI1 .Name = oli22.Name;
                    newCI1 .Offering__c = oli22.Product2.Id;
                    newCI1 .Quantity__c = oli22.Quantity;
                    newCI1 .Price__c= oli22.UnitPrice;
                    newCI1 .Item__c = oli22.Product2.ProductCode;
                    existingItems.add(newCI1 );    
                }else{
                    Contract_Items__c newCi = new Contract_Items__c();
                    newCi .Name = oli22.Name;
                    newCi .Offering__c = oli22.Product2.Id;
                    newCi .Contract__c = OppIdToContractId .get(oli22.OpportunityId);
                    newCi .Quantity__c = oli22.Quantity;
                    newCi .Price__c = oli22.UnitPrice;
                    newCi .Item__c = oli22.Product2.ProductCode;
                    existingItems.add(newCi );
                }
            }
           
		   if(existingItems.size() > 0)
              upsert existingItems ; //(Here i'm facing error)
        }
        
        
    }
}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Add the records to Map and while inserting or updating insert the map.values .

Please find the below stack exchange as your reference.

https://salesforce.stackexchange.com/questions/141345/update-records-using-map-values

If this question helps, Mark it as best answer. If you have any doubts please feel free to post the comment.

Thanks,
 
KMK91KMK91
I'm facing issue while upsert DML operation. i was using Map only
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you share the code so we can check where exact the issue is occuring from.

Thanks,
 
mukesh guptamukesh gupta
Hi,

Please follow below code:-
 
trigger ContractinsertandUpdate on Contract (before insert, before update,after insert, after update) {

    Map< Id , Id > OppIdToContractId = new Map< Id , Id >();
    List<OpportunityLineItem> OpportunityLineItemList = new List<OpportunityLineItem>();
    Set<ID> oppIds = new Set<ID>();
    List<Contract_Items__c> OldCIs = new List<Contract_Items__c>();
    Map<string,Contract_Items__c> mapOliItems =new  Map<string,Contract_Items__c>();

    if(Trigger.isAfter)
    {
  
        for(Contract contract :trigger.new){
            
            OppIdToContractId .put(contract .Opportunity__c,contract .Id);
            //oppIds.add(contract .Opportunity__c);  
        }

        OpportunityLineItemList = [select Id,Name,Product2.Name,Product2.Id,Product2.ProductCode, Quantity,UnitPrice,
                   CurrencyIsoCode,OpportunityId from opportunityLineItem
                   where OpportunityId IN :OppIdToContractId.keySet() ];
        
        
        OldCIs = [select Id, name,Item__c from Contract_Items__c where contract__c IN :trigger.new ];
        
        for(Contract_Items__c oItems :OldCIs ){
          
            mapOliItems .put(oItems .Item__c ,oItem);
        }
              if(OldCIs .size() == 0){
            if(OpportunityLineItemList .size()>0){
			    list<Contract_Items__c> newItems = new List<Contract_Items__c>();

                for(opportunityLineItem oli :OpportunityLineItemList ){
                    Contract_Items__c CI = new Contract_Items__c();
                    CI .Name = oli.Name;
                    CI .Offering__c = oli.Product2.Id;
                    CI .Contract__c = OppIdToContractId .get(oli.OpportunityId);
                    CI .Quantity__c = oli.Quantity;
                    CI .Price__c= oli.UnitPrice;
                    CI .Item__c = oli.Product2.ProductCode;
                    newItems .add(CI ); 
                }
				if(newItems.size() > 0)
					insert newItems ;
            }
        }else{
			list<Contract_Items__c> existingItems = new List<Contract_Items__c>();

            for(opportunityLineItem oli22 :OpportunityLineItemList ){
              
                if(mapOliItems .containsKey(oli22.Product2.ProductCode)){
                    Contract_Items__c newCI1 = mapOliItems .get(oli22.Product2.ProductCode);
                    newCI1 .Name = oli22.Name;
                    newCI1 .Offering__c = oli22.Product2.Id;
                    newCI1 .Quantity__c = oli22.Quantity;
                    newCI1 .Price__c= oli22.UnitPrice;
                    newCI1 .Item__c = oli22.Product2.ProductCode;
                    existingItems.add(newCI1 );    
                }else{
                    Contract_Items__c newCi = new Contract_Items__c();
                    newCi .Name = oli22.Name;
                    newCi .Offering__c = oli22.Product2.Id;
                    newCi .Contract__c = OppIdToContractId .get(oli22.OpportunityId);
                    newCi .Quantity__c = oli22.Quantity;
                    newCi .Price__c = oli22.UnitPrice;
                    newCi .Item__c = oli22.Product2.ProductCode;
                    existingItems.add(newCi );
                }
            }
           
		   if(existingItems.size() > 0)
              upsert existingItems ; //(Here i'm facing error)
        }
        
        
    }
}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 
This was selected as the best answer