0

I have wrote this below code to create order when opportunity stage is closed/Won 100%.

Problem is I am getting following error in below form in line number 8,

Too many SOQL queries: 101

I think i wrote this trigger with bulky but not sure what i missed up or what i did wrong for this code, can any one please look into this code ?

trigger OrderOfOpportunity on Opportunity (after update) {
if(trigger.isUpdate && Trigger.isAfter){
    Map<Id,Opportunity> mapOldOrderToOpp = Trigger.oldMap;
    Map<Id,Opportunity> mapNewOrderToOpp = Trigger.newMap;
    List<Order> orderList = new List<Order>();        

    if(mapNewOrderToOpp.size() > 0){
        /* This is line number 8 */
        List<opportunity> oList = [SELECT Id, CurrencyIsoCode, Type, AccountId, ContractId, stageName, RecordTypeId , sub_tipo__c, (select id from Orders),(Select Id, SBQQ__Product__c, SBQQ__Quantity__c, SBQQ__ListPrice__c from Quote_Lines__r), Quote_line_Product_Name__c FROM opportunity WHERE id IN : mapNewOrderToOpp.keyset()];
        List<orderitem> oitemList = new List<orderitem>();
        Id prbkId = [Select Id from Pricebook2 where Name = 'Lista de Precios CPQ' Limit 1].Id;
        Order order;
        if(!oList.isEmpty()){
            for(opportunity op : olist){
                if(op.Orders.isempty() && op.stageName != null && mapOldOrderToOpp.get(op.Id).stageName != op.stageName && op.stageName == 'Ganada/Firma de contrato' && op.RecordTypeId == '012700000001ix9'){
                    order = new Order();
                    order.EffectiveDate = Date.TODAY();
                    order.AccountId = op.AccountId;
                    order.OpportunityId = op.Id;
                    order.Status = 'Stand by';
                    order.Type = op.sub_tipo__c;
                    order.CurrencyIsoCode = op.CurrencyIsoCode;
                    order.PriceBook2Id = prbkId;
                    orderList.add(order);
                }
            }
        }
        if(orderList.size() > 0)
            insert orderList;
    }
}
}
7
  • 1
    Besides the inefficiencies in your code, this particular trigger isn't why your code ran into the problem. In other words, it's the symptom, not the cause.
    – sfdcfox
    Sep 20, 2017 at 18:40
  • @sfdcfox, Did you mean that something else in my org is causing and conflicting with each other and i am getting error from this trigger ? Sep 20, 2017 at 18:45
  • 1
    @WantToBeACoder Yes, you'll want to get some debug logs from your org when it's failing and look at the summary information. See Setting Up Debug Logging, set Database and Profiling to the highest levels to get the information you need.
    – sfdcfox
    Sep 20, 2017 at 18:50
  • 2
    As sfdcfox has alluded to, your query on line 8 isn't the problem. Resolving this will require you to spend a good deal of time digging through debug logs to figure out what exactly is being run, and in what order. From there, you can start looking for places where you can consolidate queries. Probably the biggest red flag though is that your org uses both Process Builder and Triggers. PB and Triggers don't mix very well, so if I had to bet on the biggest contributor to your issue here, PB would be it. I'd suggest converting those to triggers.
    – Derek F
    Sep 20, 2017 at 19:28

0

Browse other questions tagged .