3

I am getting this error System.LimitException: Too many SOQL queries: 101 when I run the class bellow. It seems like it is affecting all the triggers linked to NRProducts__c Object(This object has 45,000 records but the SOQL query should only return 7,000 ). I have a SetInterval JQuery function in the VF Page that clicks the a button every 20 seconds. My question is if I am making any mistake in the Classs code, or any ideas why this is happening. Thank you!

this is the whole error message:

  System.LimitException: Too many SOQL queries: 101
   Error is in expression '{!CreatePlan}' in component <apex:commandButton> in page   inventoryamzipa

  Trigger.InventoryAdjCountsONProducts: line 4, column 1

This is the class:

    public class InventoryAMZAPIController2{
    public String Debug{ get; set; }

    public InventoryAMZAPI2.CreateInboundShipmentPlanResult NewPlan{ get; set; }
    public List<InboundShipmentPlanRequestItem> RemainingItems{ get; set; } 

    public void CreatePlan(){

     Date target_date = date.today();

     List<NRProducts__c> details = [SELECT NRName__c, AMZ_Inventory_Update__c, Active__c, isFBA__c  FROM NRProducts__c WHERE isFBA__c=true AND Active__c = true AND AMZ_Inventory_Update__c != :target_date  LIMIT 10];
        Map<String, InboundShipmentPlanRequestItem> itemsMap = new Map<String, InboundShipmentPlanRequestItem>();
        if(!details.isEmpty()){
            RemainingItems = new List<InboundShipmentPlanRequestItem>();
            for(NRProducts__c rl : details){
                InboundShipmentPlanRequestItem item = new InboundShipmentPlanRequestItem(rl.NRName__c);
                itemsMap.put(rl.NRName__c, item);
                RemainingItems.add(item);
            }

            //request values    
            NewPlan = InventoryAMZAPI2.CreateInboundShipmentPlan(
                RemainingItems,
                'Basic'
            );

            Map<String, String> soDetailSkuMap = new Map<String, String>();  
            for(InventoryAMZAPI2.InboundShipmentPlan p : NewPlan.InboundShipmentPlans){
                soDetailSkuMap.put(p.SellerSKU, p.InStockSupplyQuantity  );  
            }

            //Assign values to products
            List<NRPRoducts__c> nr = [Select NRName__c FROM NRProducts__c Where NRName__c  IN: soDetailSkuMap.keySet()];
            for(NRPRoducts__c detail : nr){
                detail.AMZ_Inventory__c = soDetailSkuMap.get(detail.NRName__c );
                detail.AMZ_Inventory_Update__c= date.today();

                update detail;   
            }


        } else{

        Debug = 'Done';

        }

    }

    public class InboundShipmentPlanRequestItem extends InventoryAMZAPI2.InboundShipmentPlanRequestItem{
        public InboundShipmentPlanRequestItem(String sellerSKU){
            super(sellerSKU);
        }
    }
}
0

2 Answers 2

1

I have updated the code with bulk update.Here I have created a new List which holds all the updated records and is bulk updated at once.Hope it helps

            //Assign values to products
            List<NRPRoducts__c> nr = [Select NRName__c FROM NRProducts__c Where NRName__c  IN: soDetailSkuMap.keySet()];
            //List which contains updated records
            List<NRPRoducts__c> updatedNrList = new List<NRPRoducts__c>();
            for(NRPRoducts__c detail : nr){
                detail.AMZ_Inventory__c = soDetailSkuMap.get(detail.NRName__c );
                detail.AMZ_Inventory_Update__c= date.today();
                updatedNrList.add(detail);

            }
            update updatedNrList;
2
  • 2
    As all records are being updated the second list is redundant in this case.
    – Keith C
    Commented Apr 17, 2014 at 16:54
  • Thank you guys for your help, both ways work perfectly. Very much appreciate it!
    – Carlos
    Commented Apr 17, 2014 at 17:22
2

one thing that explicitly i found is the update statement inside the for loop. which you should take out of the loop and find a way to bulk update.

for(NRPRoducts__c detail : nr){
            detail.AMZ_Inventory__c = soDetailSkuMap.get(detail.NRName__c );
            detail.AMZ_Inventory_Update__c= date.today();

            update detail;   
        }
4
  • BasKaran, that was my guess but how can I do a bulk update. Any ideas?
    – Carlos
    Commented Apr 17, 2014 at 16:29
  • 6
    @Carlos Delete update detail and put update nr immediately after the nr loop.
    – Keith C
    Commented Apr 17, 2014 at 16:37
  • Keith, I will try that right now I will let you know. Thanks!
    – Carlos
    Commented Apr 17, 2014 at 16:39
  • Hi Keith, update nr made the magic. Thank you so much
    – Carlos
    Commented Apr 17, 2014 at 17:17

Not the answer you're looking for? Browse other questions tagged .