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
Phuc Nguyen 18Phuc Nguyen 18 

Having issue with mapping. Getting duplicate Ids error

I have delete batch that I would like to improve by using the existing mapping(m_finNums) but when I did I would get a duplicate id error on update.  I am looking at 3 fields on FInance record and to match them with the ones on teh DCON record. 
So to get around it I created 2 for loops which seems ineffecient. Any suggestions why I am getting the error would be appreciated
global class BatchFinanceUpdateFromDCON implements Database.Batchable<sObject> {

    global Database.QueryLocator start(Database.BatchableContext BC)
    {        
        Date yesterday = System.today() - 2;
        String query = 'SELECT Id, Requisition_Number__c, PO_Line_Description__c, DCONFinanceKeyField__c, PO_Number__c, PO_Line_Number__c,' +
                      'Goods_Received_Date__c,Record_Marked_for_Deletion__c FROM DCON_Cube_Data__c WHERE Record_Marked_for_Deletion__c = false AND '+
                      'LastModifiedDate > yesterday AND (Requisition_Number__c != null AND PO_Line_Description__c != null) AND ' +
                      '(PO_Number__c != null OR PO_Line_Number__c != Null OR Goods_Received_Date__c != null)';

       return Database.getQueryLocator(query);
    }
  
    global void execute(Database.BatchableContext BC, List<SObject> scope)
    {                   
        List<DCON_Cube_Data__c> matchDCONAndFin = (List<DCON_Cube_Data__c>)scope;
        Set<String> odapIds = new Set<String>();
        List<Finance__c> finListToUpdate = new List<Finance__c>();
        List<Finance__c> listOfFinance = new List<Finance__c>();
        List<Finance__c> listOfFinanceAllValues = new List<Finance__c>();
        List<DCON_Cube_Data__c> listOfDCONToDelete = new List<DCON_Cube_Data__c>();
        List<DCON_Cube_Data__c> DCONListToUpdate = new List<DCON_Cube_Data__c>();
        Date yesterday = System.today() - 2;

        for (DCON_Cube_Data__c DCONs : matchDCONAndFin){
            if(DCONs.PO_Line_Description__c != null && DCONs.Requisition_Number__c != null){
                odapIds.add(DCONs.DCONFinanceKeyField__c);
            }
        }

        if (!odapIds.isEmpty()) {
            listOfFinance = [SELECT Id, finDCONKeyField__c, PO_Number_Custom__c, PO_Line_Number_Custom__c, Goods_Received_Date__c ,Actual_Date__c
                                                              FROM Finance__c 
                                                              WHERE (PO_Number_Custom__c = null OR PO_Line_Number_Custom__c = Null OR Goods_Received_Date__c = null) 
                                                              AND finDCONKeyField__c in : odapIds];
        }

        Map<String, Finance__c> m_finNums = new  Map<String, Finance__c>();
        Map<String, DCON_Cube_Data__c> o_DCONNums = new  Map<String, DCON_Cube_Data__c>();
    
        for(Finance__c fins : listOfFinance){
            m_finNums.put(fins.finDCONKeyField__c, fins);
        }

         for(DCON_Cube_Data__c DCON : matchDCONAndFin){
             o_DCONNums.put(DCON.DCONFinanceKeyField__c, DCON);
         }
         
        for (DCON_Cube_Data__c DCONs : o_DCONNums.values()){                                      
            for(Finance__c finObj : listOfFinance){
                if(finObj.finDCONKeyField__c == DCONs.DCONFinanceKeyField__c ){    
                    if(finObj.Actual_Date__c == null){
                        finObj.Actual_Date__c = System.today();
                    }
                        if(DCONs.PO_Number__c != null && finObj.PO_Number_Custom__c == null){
                                finObj.PO_Number_Custom__c = DCONs.PO_Number__c;
                        }
                            if(DCONs.PO_Line_Number__c != null && finObj.PO_Line_Number_Custom__c == null){
                                finObj.PO_Line_Number_Custom__c = DCONs.PO_Line_Number__c;
                            }
                                if(DCONs.Goods_Received_Date__c != null && finObj.Goods_Received_Date__c == null){
                                    finObj.Goods_Received_Date__c = DCONs.Goods_Received_Date__c;
                                }
                        finListToUpdate.add(finObj);             
                } 
            } 
        }

        if(!finListToUpdate.isEmpty()){
            Database.update(finListToUpdate, false);
        }               
   }

    global void finish(Database.BatchableContext BC){
        BatchUpdateDCONForDeletion bd = new BatchUpdateDCONForDeletion();
        Database.executeBatch(bd, 200);
   }    
}

Thank you,
P
Best Answer chosen by Phuc Nguyen 18
Nitin sharma 425Nitin sharma 425
Usually when u get Duplicate Id error then U hav to convert it into Map and then Update which will eliminate Duplicates.
Just a random example.

Map<Id,Account>MapofAcc=new Map<id,Account>()
MapofAcc.putall(<List> of records which u wants to update);
Update MapofAcc.values();