+ Start a Discussion
Lavanya Ponniah 3Lavanya Ponniah 3 

How to rectify that error for the below trigger?

I am getting error ERROR: InstalledHardware: execution of BeforeInsertcaused by: System.ListException: Duplicate id in list: 801O00000003HOIIA2Trigger.InstalledHardware: line 32, column 1

trigger InstalledHardware on Vehicle_Details__c (before insert)
{
    list<Order> listup=new list<Order>();
    Set<Id> setOrderIds = new Set<Id>();
    Map<Id,Order> mapIdWithOrder = new Map<Id,Order>();
    for(Vehicle_Details__c V:trigger.new){
        if(v.Order__c != null){
           setOrderIds.add(v.Order__c); 
        }
    }
    if(!setOrderIds.isEmpty()){
        for(Order ordr :[select Order_Number__c,Finished_HardWare__c,Left_Hardware__c,Total_Hardware_Type_Ordered_Products__c from Order where id IN :setOrderIds]){
            mapIdWithOrder.put(ordr.Id,ordr);    
        }
    }
    for(Vehicle_Details__c V:trigger.new){
        if(v.Order__c != null && mapIdWithOrder != null && mapIdWithOrder.get(v.Order__c) != null){
           Order ordr = mapIdWithOrder.get(v.Order__c);
           v.Pending_Hardware_Installations_Order__c = ordr.Left_Hardware__c;
           v.Total_Hardware_Quantity_for_Order__c = ordr.Total_Hardware_Type_Ordered_Products__c; 
           ordr.Finished_HardWare__c = ordr.Finished_HardWare__c + V.Total_Hardware_Installed__c;
           listup.add(ordr);
        }
    }
    
    if(listup.size() > 0)
    {
        update listup;
    }
}
Sagar PareekSagar Pareek
The list listup is having duplicates in it you need to rectify the code using a set or map. 
Shaijan ThomasShaijan Thomas
Hi Lavanya,
  I don't think the error is getting from this trigger code.
1. Check Order object triggers and its related helper classes
2. Check Other triggers of Vehicle Detail Object and helper classes
Thanks
Shaijan
Bhanu MaheshBhanu Mahesh
Hi Lavanya,

Try this

trigger InstalledHardware on Vehicle_Details__c (before insert)
{
    list<Order> listup=new list<Order>();
    Set<Id> setOrderIds = new Set<Id>();
    Map<Id,Order> mapIdWithOrder = new Map<Id,Order>();
    for(Vehicle_Details__c V:trigger.new){
        if(v.Order__c != null){
           setOrderIds.add(v.Order__c); 
        }
    }
    if(!setOrderIds.isEmpty()){
        for(Order ordr :[select Order_Number__c,Finished_HardWare__c,Left_Hardware__c,Total_Hardware_Type_Ordered_Products__c from Order where id IN :setOrderIds]){
            mapIdWithOrder.put(ordr.Id,ordr);    
        }
    }
    for(Vehicle_Details__c V:trigger.new){
        if(v.Order__c != null && mapIdWithOrder != null && mapIdWithOrder.get(v.Order__c) != null){
           Order ordr = mapIdWithOrder.get(v.Order__c);
           v.Pending_Hardware_Installations_Order__c = ordr.Left_Hardware__c;
           v.Total_Hardware_Quantity_for_Order__c = ordr.Total_Hardware_Type_Ordered_Products__c; 
           ordr.Finished_HardWare__c = ordr.Finished_HardWare__c + V.Total_Hardware_Installed__c;
           //listup.add(ordr);
           mapIdWithOrder.put(v.Order__c,ordr)
        }
    }
    
    /*if(listup.size() > 0)
    {
        update listup;
    }*/
    if(mapIdWithOrder.values().size() > 0)
    {
        update mapIdWithOrder.values();
    }
}

Regards,
Bhanu Mahesh