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.

This question already has an answer here:

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);
        }
    }
}
share|improve this question

marked as duplicate by Christian Deckert, Bachovski, AtulRajguru9, mast0r, Samuel De Rycke Aug 11 at 11:25

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2 Answers 2

up vote 1 down vote accepted

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;
share|improve this answer
2  
As all records are being updated the second list is redundant in this case. –  Keith C Apr 17 at 16:54
    
Thank you guys for your help, both ways work perfectly. Very much appreciate it! –  Carlos Apr 17 at 17:22

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;   
        }
share|improve this answer
    
BasKaran, that was my guess but how can I do a bulk update. Any ideas? –  Carlos Apr 17 at 16:29
6  
@Carlos Delete update detail and put update nr immediately after the nr loop. –  Keith C Apr 17 at 16:37
    
Keith, I will try that right now I will let you know. Thanks! –  Carlos Apr 17 at 16:39
    
Hi Keith, update nr made the magic. Thank you so much –  Carlos Apr 17 at 17:17

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