Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.
trigger Order_AIUD on Order (after insert, after update, after delete) {Map<Id, List<Order>> acctIdOpptyListMap = new Map<Id, List<Order>>();
    Set<Id> acctIds = new Set<Id>();
    List<Order> opptyList = new List<Order>();
    if(trigger.isUpdate || trigger.isInsert){
        for(Order oppty : trigger.New){
            if(oppty.AccountId != null){
                acctIds.add(oppty.AccountId);
            }
        }    
    }
    if(trigger.isDelete){
        for(Order oppty : trigger.old){
            if(oppty.AccountId != null){
                acctIds.add(oppty.AccountId);
            }
        }    
    }
    if(acctIds.size() > 0){
        opptyList = [SELECT  X18x181__c, AccountId FROM Order WHERE AccountId IN : acctIds];
        for(Order oppty : opptyList){
            if(!acctIdOpptyListMap.containsKey(oppty.AccountId)){
                acctIdOpptyListMap.put(oppty.AccountId, new List<Order>());
            }
            acctIdOpptyListMap.get(oppty.AccountId).add(oppty); 
        }   
        List<Account> acctList = new List<Account>();
        acctList = [SELECT x18x18s__c FROM Account WHERE Id IN: acctIds];
        for(Account acct : acctList){
            List<Order> tempOpptyList = new List<Order>();
            tempOpptyList = acctIdOpptyListMap.get(acct.Id);
            Double totalOpptyX18x181 = 0;
            for(Order oppty : tempOpptyList){
                if(oppty.X18x181__c != null){
                    totalOpptyX18x181 += oppty.X18x181__c;
                }
            }
            acct.x18x18s__c = totalOpptyX18x181;
        }
        update acctList;
    }
    Map<Id, List<Order>> acct1Idoppty1List1Map1 = new Map<Id, List<Order>>();
    Set<Id> acct1Ids1 = new Set<Id>();
    List<Order> oppty1List1 = new List<Order>();
    if(trigger.isUpdate || trigger.isInsert){
        for(Order oppty1 : trigger.New){
            if(oppty1.AccountId != null){
                acct1Ids1.add(oppty1.AccountId);
            }
        }    
    }
    if(trigger.isDelete){
        for(Order oppty1 : trigger.old){
            if(oppty1.AccountId != null){
                acct1Ids1.add(oppty1.AccountId);
            }
        }    
    }
    if(acct1Ids1.size() > 0){
        oppty1List1 = [SELECT  X18x18_Black1__c, AccountId FROM Order WHERE AccountId IN : acct1Ids1];
        for(Order oppty1 : oppty1List1){
            if(!acct1Idoppty1List1Map1.containsKey(oppty1.AccountId)){
                acct1Idoppty1List1Map1.put(oppty1.AccountId, new List<Order>());
            }
            acct1Idoppty1List1Map1.get(oppty1.AccountId).add(oppty1); 
        }   
        List<Account> acct1List1 = new List<Account>();
        acct1List1 = [SELECT x18x18bs__c FROM Account WHERE Id IN: acct1Ids1];
        for(Account acct1 : acct1List1){
            List<Order> tempoppty1List1 = new List<Order>();
            tempoppty1List1 = acct1Idoppty1List1Map1.get(acct1.Id);
            Double totaloppty1X18x181 = 0;
            for(Order oppty1 : tempoppty1List1){
                if(oppty1.X18x18_Black1__c != null){
                    totaloppty1X18x181 += oppty1.X18x18_Black1__c;
                }
            }
            acct1.x18x18bs__c = totaloppty1X18x181;
        }
        update acct1List1;
    }
    Map<Id, List<Order>> acct2Idoppty2List2Map2 = new Map<Id, List<Order>>();
    Set<Id> acct2Ids2 = new Set<Id>();
    List<Order> oppty2List2 = new List<Order>();
    if(trigger.isUpdate || trigger.isInsert){
        for(Order oppty2 : trigger.New){
            if(oppty2.AccountId != null){
                acct2Ids2.add(oppty2.AccountId);
            }
        }    
    }
    if(trigger.isDelete){
        for(Order oppty2 : trigger.old){
            if(oppty2.AccountId != null){
                acct2Ids2.add(oppty2.AccountId);
            }
        }    
    }
    if(acct2Ids2.size() > 0){
        oppty2List2 = [SELECT  X18x18_Quilted1__c, AccountId FROM Order WHERE AccountId IN : acct2Ids2];
        for(Order oppty2 : oppty2List2){
            if(!acct2Idoppty2List2Map2.containsKey(oppty2.AccountId)){
                acct2Idoppty2List2Map2.put(oppty2.AccountId, new List<Order>());
            }
            acct2Idoppty2List2Map2.get(oppty2.AccountId).add(oppty2); 
        }   
        List<Account> acct2List2 = new List<Account>();
        acct2List2 = [SELECT X18x18qs__c FROM Account WHERE Id IN: acct2Ids2];
        for(Account acct2 : acct2List2){
            List<Order> tempoppty2List2 = new List<Order>();
            tempoppty2List2 = acct2Idoppty2List2Map2.get(acct2.Id);
            Double totaloppty2X18x181 = 0;
            for(Order oppty2 : tempoppty2List2){
                if(oppty2.X18x18_Quilted1__c != null){
                    totaloppty2X18x181 += oppty2.X18x18_Quilted1__c;
                }
            }
            acct2.X18x18qs__c = totaloppty2X18x181;
        }
        update acct2List2;
    }


 Map<Id, List<Order>> acctIdoppty3List3Map3 = new Map<Id, List<Order>>();
    Set<Id> acctIds3 = new Set<Id>();
    List<Order> oppty3List3 = new List<Order>();
    if(trigger.isUpdate || trigger.isInsert){
        for(Order oppty3 : trigger.New){
            if(oppty3.AccountId != null){
                acctIds3.add(oppty3.AccountId);
            }
        }    
    }
    if(trigger.isDelete){
        for(Order oppty3 : trigger.old){
            if(oppty3.AccountId != null){
                acctIds3.add(oppty3.AccountId);
            }
        }    
    }
    if(acctIds3.size() > 0){
        oppty3List3 = [SELECT  X15x15_White_Wipers1__c, AccountId FROM Order WHERE AccountId IN : acctIds3];
        for(Order oppty3 : oppty3List3){
            if(!acctIdoppty3List3Map3.containsKey(oppty3.AccountId)){
                acctIdoppty3List3Map3.put(oppty3.AccountId, new List<Order>());
            }
            acctIdoppty3List3Map3.get(oppty3.AccountId).add(oppty3); 
        }   
        List<Account> acctList3 = new List<Account>();
        acctList3 = [SELECT White_wipers_s__c FROM Account WHERE Id IN: acctIds3];
        for(Account acct : acctList3){
            List<Order> tempoppty3List3 = new List<Order>();
            tempoppty3List3 = acctIdoppty3List3Map3.get(acct.Id);
            Double totaloppty3X18x181 = 0;
            for(Order oppty3 : tempoppty3List3){
                if(oppty3.X15x15_White_Wipers1__c != null){
                    totaloppty3X18x181 += oppty3.X15x15_White_Wipers1__c;
                }
            }
            acct.White_wipers_s__c = totaloppty3X18x181;
        }
        update acctList3;
    }
Map<Id, List<Order>> acct4Idoppty4List4Map4 = new Map<Id, List<Order>>();
    Set<Id> acct4Ids4 = new Set<Id>();
    List<Order> oppty4List4 = new List<Order>();
    if(trigger.isUpdate || trigger.isInsert){
        for(Order oppty4 : trigger.New){
            if(oppty4.AccountId != null){
                acct4Ids4.add(oppty4.AccountId);
            }
        }    
    }
    if(trigger.isDelete){
        for(Order oppty4 : trigger.old){
            if(oppty4.AccountId != null){
                acct4Ids4.add(oppty4.AccountId);
            }
        }    
    }
    if(acct4Ids4.size() > 0){
        oppty4List4 = [SELECT  X2x31__c, AccountId FROM Order WHERE AccountId IN : acct4Ids4];
        for(Order oppty4 : oppty4List4){
            if(!acct4Idoppty4List4Map4.containsKey(oppty4.AccountId)){
                acct4Idoppty4List4Map4.put(oppty4.AccountId, new List<Order>());
            }
            acct4Idoppty4List4Map4.get(oppty4.AccountId).add(oppty4); 
        }   
        List<Account> acct4List4 = new List<Account>();
        acct4List4 = [SELECT X2x3s__c FROM Account WHERE Id IN: acct4Ids4];
        for(Account acct4 : acct4List4){
            List<Order> tempoppty4List4 = new List<Order>();
            tempoppty4List4 = acct4Idoppty4List4Map4.get(acct4.Id);
            Double totaloppty4X18x181 = 0;
            for(Order oppty4 : tempoppty4List4){
                if(oppty4.X2x31__c != null){
                    totaloppty4X18x181 += oppty4.X2x31__c;
                }
            }
            acct4.X2x3s__c = totaloppty4X18x181;
        }
        update acct4List4;
    }
     Map<Id, List<Order>> acct5Idoppty5List5Map5 = new Map<Id, List<Order>>();
    Set<Id> acct5Ids5 = new Set<Id>();
    List<Order> oppty5List5 = new List<Order>();
    if(trigger.isUpdate || trigger.isInsert){
        for(Order oppty5 : trigger.New){
            if(oppty5.AccountId != null){
                acct5Ids5.add(oppty5.AccountId);
            }
        }    
    }
    if(trigger.isDelete){
        for(Order oppty5 : trigger.old){
            if(oppty5.AccountId != null){
                acct5Ids5.add(oppty5.AccountId);
            }
        }    
    }
    if(acct5Ids5.size() > 0){
        oppty5List5 = [SELECT  X3x101__c, AccountId FROM Order WHERE AccountId IN : acct5Ids5];
        for(Order oppty5 : oppty5List5){
            if(!acct5Idoppty5List5Map5.containsKey(oppty5.AccountId)){
                acct5Idoppty5List5Map5.put(oppty5.AccountId, new List<Order>());
            }
            acct5Idoppty5List5Map5.get(oppty5.AccountId).add(oppty5); 
        }   
        List<Account> acct5List5 = new List<Account>();
        acct5List5 = [SELECT X3x10s__c FROM Account WHERE Id IN: acct5Ids5];
        for(Account acct5 : acct5List5){
            List<Order> tempoppty5List5 = new List<Order>();
            tempoppty5List5 = acct5Idoppty5List5Map5.get(acct5.Id);
            Double totaloppty5X18x181 = 0;
            for(Order oppty5 : tempoppty5List5){
                if(oppty5.X3x101__c != null){
                    totaloppty5X18x181 += oppty5.X3x101__c;
                }
            }
            acct5.X3x10s__c = totaloppty5X18x181;
        }
        update acct5List5;
    }
    Map<Id, List<Order>> acct6Idoppty6List6Map6 = new Map<Id, List<Order>>();
    Set<Id> acct6Ids6 = new Set<Id>();
    List<Order> oppty6List6 = new List<Order>();
    if(trigger.isUpdate || trigger.isInsert){
        for(Order oppty6 : trigger.New){
            if(oppty6.AccountId != null){
                acct6Ids6.add(oppty6.AccountId);
            }
        }    
    }
    if(trigger.isDelete){
        for(Order oppty6 : trigger.old){
            if(oppty6.AccountId != null){
                acct6Ids6.add(oppty6.AccountId);
            }
        }    
    }
    if(acct6Ids6.size() > 0){
        oppty6List6 = [SELECT  X3x10_Black1__c, AccountId FROM Order WHERE AccountId IN : acct6Ids6];
        for(Order oppty6 : oppty6List6){
            if(!acct6Idoppty6List6Map6.containsKey(oppty6.AccountId)){
                acct6Idoppty6List6Map6.put(oppty6.AccountId, new List<Order>());
            }
            acct6Idoppty6List6Map6.get(oppty6.AccountId).add(oppty6); 
        }   
        List<Account> acct6List6 = new List<Account>();
        acct6List6 = [SELECT X3x10bs__c FROM Account WHERE Id IN: acct6Ids6];
        for(Account acct6 : acct6List6){
            List<Order> tempoppty6List6 = new List<Order>();
            tempoppty6List6 = acct6Idoppty6List6Map6.get(acct6.Id);
            Double totaloppty6X18x181 = 0;
            for(Order oppty6 : tempoppty6List6){
                if(oppty6.X3x10_Black1__c != null){
                    totaloppty6X18x181 += oppty6.X3x10_Black1__c;
                }
            }
            acct6.X3x10bs__c = totaloppty6X18x181;
        }
        update acct6List6;
    }
    Map<Id, List<Order>> acct7Idoppty7List7Map7 = new Map<Id, List<Order>>();
    Set<Id> acct7Ids7 = new Set<Id>();
    List<Order> oppty7List7 = new List<Order>();
    if(trigger.isUpdate || trigger.isInsert){
        for(Order oppty7 : trigger.New){
            if(oppty7.AccountId != null){
                acct7Ids7.add(oppty7.AccountId);
            }
        }    
    }
    if(trigger.isDelete){
        for(Order oppty7 : trigger.old){
            if(oppty7.AccountId != null){
                acct7Ids7.add(oppty7.AccountId);
            }
        }    
    }
    if(acct7Ids7.size() > 0){
        oppty7List7 = [SELECT  X3x181__c, AccountId FROM Order WHERE AccountId IN : acct7Ids7];
        for(Order oppty7 : oppty7List7){
            if(!acct7Idoppty7List7Map7.containsKey(oppty7.AccountId)){
                acct7Idoppty7List7Map7.put(oppty7.AccountId, new List<Order>());
            }
            acct7Idoppty7List7Map7.get(oppty7.AccountId).add(oppty7); 
        }   
        List<Account> acct7List7 = new List<Account>();
        acct7List7 = [SELECT X3x18s__c FROM Account WHERE Id IN: acct7Ids7];
        for(Account acct7 : acct7List7){
            List<Order> tempoppty7List7 = new List<Order>();
            tempoppty7List7 = acct7Idoppty7List7Map7.get(acct7.Id);
            Double totaloppty7X18x181 = 0;
            for(Order oppty7 : tempoppty7List7){
                if(oppty7.X3x181__c != null){
                    totaloppty7X18x181 += oppty7.X3x181__c;
                }
            }
            acct7.X3x18s__c = totaloppty7X18x181;
        }
        update acct7List7;
    }
     Map<Id, List<Order>> acct8Idoppty8List8Map8 = new Map<Id, List<Order>>();
    Set<Id> acct8Ids8 = new Set<Id>();
    List<Order> oppty8List8 = new List<Order>();
    if(trigger.isUpdate || trigger.isInsert){
        for(Order oppty8 : trigger.New){
            if(oppty8.AccountId != null){
                acct8Ids8.add(oppty8.AccountId);
            }
        }    
    }
    if(trigger.isDelete){
        for(Order oppty8 : trigger.old){
            if(oppty8.AccountId != null){
                acct8Ids8.add(oppty8.AccountId);
            }
        }    
    }
    if(acct8Ids8.size() > 0){
        oppty8List8 = [SELECT  X3x18_Black1__c, AccountId FROM Order WHERE AccountId IN : acct8Ids8];
        for(Order oppty8 : oppty8List8){
            if(!acct8Idoppty8List8Map8.containsKey(oppty8.AccountId)){
                acct8Idoppty8List8Map8.put(oppty8.AccountId, new List<Order>());
            }
            acct8Idoppty8List8Map8.get(oppty8.AccountId).add(oppty8); 
        }   
        List<Account> acct8List8 = new List<Account>();
        acct8List8 = [SELECT X3x18bs__c FROM Account WHERE Id IN: acct8Ids8];
        for(Account acct8 : acct8List8){
            List<Order> tempoppty8List8 = new List<Order>();
            tempoppty8List8 = acct8Idoppty8List8Map8.get(acct8.Id);
            Double totaloppty8X18x181 = 0;
            for(Order oppty8 : tempoppty8List8){
                if(oppty8.X3x18_Black1__c != null){
                    totaloppty8X18x181 += oppty8.X3x18_Black1__c;
                }
            }
            acct8.X3x18bs__c = totaloppty8X18x181;
        }
        update acct8List8;
    }
      Map<Id, List<Order>> acct9Idoppty9List9Map9 = new Map<Id, List<Order>>();
    Set<Id> acct9Ids9 = new Set<Id>();
    List<Order> oppty9List9 = new List<Order>();
    if(trigger.isUpdate || trigger.isInsert){
        for(Order oppty9 : trigger.New){
            if(oppty9.AccountId != null){
                acct9Ids9.add(oppty9.AccountId);
            }
        }    
    }
    if(trigger.isDelete){
        for(Order oppty9 : trigger.old){
            if(oppty9.AccountId != null){
                acct9Ids9.add(oppty9.AccountId);
            }
        }    
    }
    if(acct9Ids9.size() > 0){
        oppty9List9 = [SELECT  X3x41__c, AccountId FROM Order WHERE AccountId IN : acct9Ids9];
        for(Order oppty9 : oppty9List9){
            if(!acct9Idoppty9List9Map9.containsKey(oppty9.AccountId)){
                acct9Idoppty9List9Map9.put(oppty9.AccountId, new List<Order>());
            }
            acct9Idoppty9List9Map9.get(oppty9.AccountId).add(oppty9); 
        }   
        List<Account> acct9List9 = new List<Account>();
        acct9List9 = [SELECT X3x4s__c FROM Account WHERE Id IN: acct9Ids9];
        for(Account acct9 : acct9List9){
            List<Order> tempoppty9List9 = new List<Order>();
            tempoppty9List9 = acct9Idoppty9List9Map9.get(acct9.Id);
            Double totaloppty9X18x181 = 0;
            for(Order oppty9 : tempoppty9List9){
                if(oppty9.X3x41__c != null){
                    totaloppty9X18x181 += oppty9.X3x41__c;
                }
            }
            acct9.X3x4s__c = totaloppty9X18x181;
        }
        update acct9List9;
    }
      Map<Id, List<Order>> acct10Idoppty10List10Map10 = new Map<Id, List<Order>>();
    Set<Id> acct10Ids10 = new Set<Id>();
    List<Order> oppty10List10 = new List<Order>();
    if(trigger.isUpdate || trigger.isInsert){
        for(Order oppty10 : trigger.New){
            if(oppty10.AccountId != null){
                acct10Ids10.add(oppty10.AccountId);
            }
        }    
    }
    if(trigger.isDelete){
        for(Order oppty10 : trigger.old){
            if(oppty10.AccountId != null){
                acct10Ids10.add(oppty10.AccountId);
            }
        }    
    }
    if(acct10Ids10.size() > 0){
        oppty10List10 = [SELECT  X3x4_Black1__c, AccountId FROM Order WHERE AccountId IN : acct10Ids10];
        for(Order oppty10 : oppty10List10){
            if(!acct10Idoppty10List10Map10.containsKey(oppty10.AccountId)){
                acct10Idoppty10List10Map10.put(oppty10.AccountId, new List<Order>());
            }
            acct10Idoppty10List10Map10.get(oppty10.AccountId).add(oppty10); 
        }   
        List<Account> acct10List10 = new List<Account>();
        acct10List10 = [SELECT X3x4bs__c FROM Account WHERE Id IN: acct10Ids10];
        for(Account acct10 : acct10List10){
            List<Order> tempoppty10List10 = new List<Order>();
            tempoppty10List10 = acct10Idoppty10List10Map10.get(acct10.Id);
            Double totaloppty10X18x181 = 0;
            for(Order oppty10 : tempoppty10List10){
                if(oppty10.X3x4_Black1__c != null){
                    totaloppty10X18x181 += oppty10.X3x4_Black1__c;
                }
            }
            acct10.X3x4bs__c = totaloppty10X18x181;
        }
        update acct10List10;
    }
     Map<Id, List<Order>> acct11Idoppty11List11Map11 = new Map<Id, List<Order>>();
    Set<Id> acct11Ids11 = new Set<Id>();
    List<Order> oppty11List11 = new List<Order>();
    if(trigger.isUpdate || trigger.isInsert){
        for(Order oppty11 : trigger.New){
            if(oppty11.AccountId != null){
                acct11Ids11.add(oppty11.AccountId);
            }
        }    
    }
    if(trigger.isDelete){
        for(Order oppty11 : trigger.old){
            if(oppty11.AccountId != null){
                acct11Ids11.add(oppty11.AccountId);
            }
        }    
    }
    if(acct11Ids11.size() > 0){
        oppty11List11 = [SELECT X3x61__c, AccountId FROM Order WHERE AccountId IN : acct11Ids11];
        for(Order oppty11 : oppty11List11){
            if(!acct11Idoppty11List11Map11.containsKey(oppty11.AccountId)){
                acct11Idoppty11List11Map11.put(oppty11.AccountId, new List<Order>());
            }
            acct11Idoppty11List11Map11.get(oppty11.AccountId).add(oppty11); 
        }   
        List<Account> acct11List11 = new List<Account>();
        acct11List11 = [SELECT X3x6s__c FROM Account WHERE Id IN: acct11Ids11];
        for(Account acct11 : acct11List11){
            List<Order> tempoppty11List11 = new List<Order>();
            tempoppty11List11 = acct11Idoppty11List11Map11.get(acct11.Id);
            Double totaloppty11X18x181 = 0;
            for(Order oppty11 : tempoppty11List11){
                if(oppty11.X3x61__c != null){
                    totaloppty11X18x181 += oppty11.X3x61__c;
                }
            }
            acct11.X3x6s__c = totaloppty11X18x181;
        }
        update acct11List11;
    }
  Map<Id, List<Order>> acct12Idoppty12List12Map12 = new Map<Id, List<Order>>();
    Set<Id> acct12Ids12 = new Set<Id>();
    List<Order> oppty12List12 = new List<Order>();
    if(trigger.isUpdate || trigger.isInsert){
        for(Order oppty12 : trigger.New){
            if(oppty12.AccountId != null){
                acct12Ids12.add(oppty12.AccountId);
            }
        }    
    }
    if(trigger.isDelete){
        for(Order oppty12 : trigger.old){
            if(oppty12.AccountId != null){
                acct12Ids12.add(oppty12.AccountId);
            }
        }    
    }
    if(acct12Ids12.size() > 0){
        oppty12List12 = [SELECT  X3x8__c, AccountId FROM Order WHERE AccountId IN : acct12Ids12];
        for(Order oppty12 : oppty12List12){
            if(!acct12Idoppty12List12Map12.containsKey(oppty12.AccountId)){
                acct12Idoppty12List12Map12.put(oppty12.AccountId, new List<Order>());
            }
            acct12Idoppty12List12Map12.get(oppty12.AccountId).add(oppty12); 
        }   
        List<Account> acct12List12 = new List<Account>();
        acct12List12 = [SELECT X3x8s__c FROM Account WHERE Id IN: acct12Ids12];
        for(Account acct12 : acct12List12){
            List<Order> tempoppty12List12 = new List<Order>();
            tempoppty12List12 = acct12Idoppty12List12Map12.get(acct12.Id);
            Double totaloppty12X18x181 = 0;
            for(Order oppty12 : tempoppty12List12){
                if(oppty12.X3x8__c != null){
                    totaloppty12X18x181 += oppty12.X3x8__c;
                }
            }
            acct12.X3x8s__c = totaloppty12X18x181;
        }
        update acct12List12;
    }
      Map<Id, List<Order>> acct13Idoppty13List13Map13 = new Map<Id, List<Order>>();
    Set<Id> acct13Ids13 = new Set<Id>();
    List<Order> oppty13List13 = new List<Order>();
    if(trigger.isUpdate || trigger.isInsert){
        for(Order oppty13 : trigger.New){
            if(oppty13.AccountId != null){
                acct13Ids13.add(oppty13.AccountId);
            }
        }    
    }
    if(trigger.isDelete){
        for(Order oppty13 : trigger.old){
            if(oppty13.AccountId != null){
                acct13Ids13.add(oppty13.AccountId);
            }
        }    
    }
    if(acct13Ids13.size() > 0){
        oppty13List13 = [SELECT  Boxes_of_Socks1__c, AccountId FROM Order WHERE AccountId IN : acct13Ids13];
        for(Order oppty13 : oppty13List13){
            if(!acct13Idoppty13List13Map13.containsKey(oppty13.AccountId)){
                acct13Idoppty13List13Map13.put(oppty13.AccountId, new List<Order>());
            }
            acct13Idoppty13List13Map13.get(oppty13.AccountId).add(oppty13); 
        }   
        List<Account> acct13List13 = new List<Account>();
        acct13List13 = [SELECT Boxes_of_socks_s__c FROM Account WHERE Id IN: acct13Ids13];
        for(Account acct13 : acct13List13){
            List<Order> tempoppty13List13 = new List<Order>();
            tempoppty13List13 = acct13Idoppty13List13Map13.get(acct13.Id);
            Double totaloppty13X18x181 = 0;
            for(Order oppty13 : tempoppty13List13){
                if(oppty13.Boxes_of_Socks1__c != null){
                    totaloppty13X18x181 += oppty13.Boxes_of_Socks1__c;
                }
            }
            acct13.Boxes_of_socks_s__c = totaloppty13X18x181;
        }
        update acct13List13;
    }
     Map<Id, List<Order>> acct14Idoppty14List14Map14 = new Map<Id, List<Order>>();
    Set<Id> acct14Ids14 = new Set<Id>();
    List<Order> oppty14List14 = new List<Order>();
    if(trigger.isUpdate || trigger.isInsert){
        for(Order oppty14 : trigger.New){
            if(oppty14.AccountId != null){
                acct14Ids14.add(oppty14.AccountId);
            }
        }    
    }
    if(trigger.isDelete){
        for(Order oppty14 : trigger.old){
            if(oppty14.AccountId != null){
                acct14Ids14.add(oppty14.AccountId);
            }
        }    
    }
    if(acct14Ids14.size() > 0){
        oppty14List14 = [SELECT  Hydros__c, AccountId FROM Order WHERE AccountId IN : acct14Ids14];
        for(Order oppty14 : oppty14List14){
            if(!acct14Idoppty14List14Map14.containsKey(oppty14.AccountId)){
                acct14Idoppty14List14Map14.put(oppty14.AccountId, new List<Order>());
            }
            acct14Idoppty14List14Map14.get(oppty14.AccountId).add(oppty14); 
        }   
        List<Account> acct14List14 = new List<Account>();
        acct14List14 = [SELECT Hydros_S__c FROM Account WHERE Id IN: acct14Ids14];
        for(Account acct14 : acct14List14){
            List<Order> tempoppty14List14 = new List<Order>();
            tempoppty14List14 = acct14Idoppty14List14Map14.get(acct14.Id);
            Double totaloppty14X18x181 = 0;
            for(Order oppty14 : tempoppty14List14){
                if(oppty14.Hydros__c != null){
                    totaloppty14X18x181 += oppty14.Hydros__c;
                }
            }
            acct14.Hydros_S__c = totaloppty14X18x181;
        }
        update acct14List14;
    }
     Map<Id, List<Order>> acct15Idoppty15List15Map15 = new Map<Id, List<Order>>();
    Set<Id> acct15Ids15 = new Set<Id>();
    List<Order> oppty15List15 = new List<Order>();
    if(trigger.isUpdate || trigger.isInsert){
        for(Order oppty15 : trigger.New){
            if(oppty15.AccountId != null){
                acct15Ids15.add(oppty15.AccountId);
            }
        }    
    }
    if(trigger.isDelete){
        for(Order oppty15 : trigger.old){
            if(oppty15.AccountId != null){
                acct15Ids15.add(oppty15.AccountId);
            }
        }    
    }
    if(acct15Ids15.size() > 0){
        oppty15List15 = [SELECT  Gloves__c, AccountId FROM Order WHERE AccountId IN : acct15Ids15];
        for(Order oppty15 : oppty15List15){
            if(!acct15Idoppty15List15Map15.containsKey(oppty15.AccountId)){
                acct15Idoppty15List15Map15.put(oppty15.AccountId, new List<Order>());
            }
            acct15Idoppty15List15Map15.get(oppty15.AccountId).add(oppty15); 
        }   
        List<Account> acct15List15 = new List<Account>();
        acct15List15 = [SELECT Gloves_s__c FROM Account WHERE Id IN: acct15Ids15];
        for(Account acct15 : acct15List15){
            List<Order> tempoppty15List15 = new List<Order>();
            tempoppty15List15 = acct15Idoppty15List15Map15.get(acct15.Id);
            Double totaloppty15X18x181 = 0;
            for(Order oppty15 : tempoppty15List15){
                if(oppty15.Gloves__c != null){
                    totaloppty15X18x181 += oppty15.Gloves__c;
                }
            }
            acct15.Gloves_s__c = totaloppty15X18x181;
        }
        update acct15List15;
    }



}
share|improve this question
    
Edited your code..that was the first step of getting help here. –  Richard N Jun 30 at 22:51
1  
You don't seem to have SOQL inside FOR loops which is the usual suspect. But how are your preventing your trigger from being executed recursively? –  Richard N Jun 30 at 22:53
1  
Also check your test code. Are the test.StartTest() and Test.Stop() in the right places? –  Richard N Jun 30 at 22:55
    
possible duplicate of System.Exception: Too many SOQL queries: 101 –  Christian Deckert Aug 14 at 22:05

3 Answers 3

up vote 4 down vote accepted

Background

Based on the length of that trigger I'm guessing you're working in a pretty complex, mature org. As you load up on trigger code each object DML statement can accumulate a lot of downstream SOQL statements, e.g. an account insert can lead to 10 SOQL statements, adding a contact adds another 5 statements. Where this first start to strain the organization is test methods which have to create a lot of test data.

So what to do? In order of simplicity:

  1. Rule out SOQL/DML in for loops
  2. Use start/stopTest to increase limits in failing tests
  3. Break up failing tests into smaller functional units that require less data
  4. Review triggers for repeated actions

In your case, (1) sounds like it's not an issue. (2) is where I'd start, for each test you get double governor limits, one oustide the start/stopTest block, and one inside. Manipulating where you put the startTest earlier in the test to shift more of the data creation into the "test" governor limit. If that fails (3) may help since each test method gets it's own test limits. Finally, if you're all out of luck there, it's time to roll up the sleeves and do some serious refactoring of your triggers. If you get here, it will be a long road.

share|improve this answer

There appear to be 16 separate updates on lists of Accounts.

E.g.

update acctList;
update acct1List1;
update acct2List2;
update acctList3;
update acct4List4;
update acct5List5;
// and so on.
update acct15List15;

When you combine the 32 possible selects in this trigger with the SOQL calls from any Account triggers multiplied by upto 16 separate updates you could easily reach the 101 limit.

At a minimum, consider only having a single call to update Account records. Then check the bulkification of the triggers on Account as well.

One other thing, check if your test cases are using @IsTest(seeAllData=true). Ideally they wouldn't and instead rely on scripted test data.

share|improve this answer

You have 32 SOQL statements in that one Trigger. This means all it could take is 4 Order objects being upserted/deleted and you'll exceed the 101 limit.

To me, it looks like you have a fundamental problem with your object model. It looks like you have every product/sku entered as a field on the Order and/or Account object. You really need to review this because it's definitely not scalable.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.