function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ayyagari RameshAyyagari Ramesh 

Too many SOQL queries: 101 Issue

To fix the exception: System.LimitException: Too many SOQL queries: 101 made changes to the apex class as below. Can anyone please highlight the issue in it? as i am getting another error while saving-Method does not exist or incorrect signature: void add(String) from the type List<Order>

Apex Class-
Public class QTB_CreateOrderItem {
    public static final string className = QTB_CreateOrderItem.class.getName();
    public static boolean throwExplicitException = false;
    // Calling this method from flow to create orderitems based on selected products.
    @InvocableMethod
   Public static void updateOrderItemDetails(list <flowInputs> request)
    {   
        List<OrderItem> OrderItemlst = new List<OrderItem>();
        List <PriceBookEntry> book = new  List <PriceBookEntry>();
        List<Order> NewCrOrder = new List<Order>();
        String errorIds = '';
        List<Order> NewCrOrder1 = new List<Order>();
       
        
        For(flowInputs flow:request)
        {
            NewCrOrder1.add(flow.newOrderCreated);
            
            book=[select id, name,Product2Id,Product2.SBQQ__ChargeType__c,Product2.SBQQ__BillingFrequency__c,Product2.SBQQ__BillingType__c,Product2.blng__BillingRule__c,Product2.blng__TaxRule__c,Product2.blng__RevenueRecognitionRule__c,Product2.SBQQ__SubscriptionTerm__c from PricebookEntry where ProductCode in :flow.productCodes and Pricebook2Id = :flow.oldOrderData.Pricebook2Id ];
            system.debug('Retrieved PRbentry ----------'+ book.size());      
        }
        
        NewCrOrder1 = [Select id,name,EffectiveDate,EndDate,QTB_Legal_Entity_Country__c,Account.QTB_Contract_Entity__c from order  where id IN :NewCrOrder];    
        

            
        
        try {
            if (throwExplicitException) {
                system.debug('NullPointer');
                throw new NullPointerException();
            } 
            
            List<blng__BillingTreatment__c> sgListBillingTreat=[Select id, name,blng__BillingRule__r.id from blng__BillingTreatment__c Where Name='No Invoice Billing Treatment' LIMIT 1];
            if(book.size()>0)  {  
                For(pricebookentry pr : book)
                  {  
                    OrderItem temporderitem = new OrderItem();
                    temporderitem.OrderId = NewCrOrder[0].id;
                    temporderitem.blng__BillableUnitPrice__c= 0;
                    temporderitem.Product2Id = pr.Product2.Id;
                    temporderitem.Quantity = 1;
                    temporderitem.UnitPrice = 0;    
                    temporderitem.blng__HoldBilling__c = 'No';   
                    temporderitem.blng__TaxCountry__c = NewCrOrder[0].QTB_Legal_Entity_Country__c;
                    temporderitem.ServiceDate = NewCrOrder[0].EffectiveDate;
                    temporderitem.EndDate = NewCrOrder[0].EndDate;
                    temporderitem.SBQQ__BillingFrequency__c = 'Monthly';
                    temporderitem.SBQQ__BillingType__c = 'Arrears';
                    temporderitem.SBQQ__ChargeType__c  = 'Recurring';  
                    temporderitem.SBQQ__ContractAction__c  = 'New'; 
                    temporderitem.SBQQ__ContractingMethod__c  = 'Inherit';
                    temporderitem.SBQQ__SubscriptionTerm__c= 1;    
                    temporderitem.SBQQ__DefaultSubscriptionTerm__c= 1;
                    temporderitem.SBQQ__OrderedQuantity__c= 1;
                    temporderitem.SBQQ__PricingMethod__c='List';
                    temporderitem.SBQQ__ProductSubscriptionType__c = 'One-Time'; 
                    temporderitem.SBQQ__SubscriptionType__c = 'One-Time';
                    temporderitem.SBQQ__Status__c = 'Draft';   
                    temporderitem.SBQQ__SubscriptionPricing__c='Fixed Price';
                    temporderitem.PricebookEntryId = pr.Id;
                    temporderitem.blng__BillingRule__c =pr.Product2.blng__BillingRule__c;
                    temporderitem.blng__BillingTreatment__c = sgListBillingTreat[0].id;
                    temporderitem.blng__LegalEntity__c = NewCrOrder[0].Account.QTB_Contract_Entity__c;
                    temporderitem.blng__RevenueRecognitionRule__c  = pr.Product2.blng__RevenueRecognitionRule__c;
                    temporderitem.blng__TaxRule__c = pr.Product2.blng__TaxRule__c;
                    OrderItemlst.add(temporderitem);    
                   }
            }
            Insert OrderItemlst;
            system.debug('Inserted Items ======' +OrderItemlst.size());
            List<Id> OrdidtoLoad = new List<Id>();
            OrdidtoLoad.add(NewCrOrder[0].id);
            //To update order item for seperate invoicing 
            list<OrderItem> lstupdate= new list<OrderItem>();
            list<OrderItem> itemlst = new list<OrderItem>();
            itemlst=[Select id,blng__InvoiceGrouping__c,blng__InvoiceGroupId__c,Order.OrderNumber from OrderItem where order.id IN :OrdidtoLoad];
            system.debug('Total Selected to update:'+ itemlst.size());
            for(OrderItem ls : itemlst)
            {
                ls.blng__InvoiceGrouping__c= 'Invoice Group ID';
                ls.blng__InvoiceGroupId__c= ls.Order.OrderNumber;
                lstupdate.add(ls);
                
            }
            system.debug('Total Selected to update:'+ lstupdate.size());  
            update lstupdate;
            //To Update matching attribute on usage summary related to newly created orderItems
            List<blng__UsageSummary__c  > Uslsttoupdate =new List<blng__UsageSummary__c>();
            List<blng__UsageSummary__c  > Uslst = new List<blng__UsageSummary__c  >();
            Uslst =[Select id,blng__MatchingAttribute__c,blng__Order__r.OrderNumber from blng__UsageSummary__c Where blng__Order__r.id IN :OrdidtoLoad];
            system.debug('Size+++++++++++++++:'+ Uslst.size());
            for (blng__UsageSummary__c us :Uslst)
            {
                us.blng__MatchingAttribute__c = us.blng__Order__r.OrderNumber;
                Uslsttoupdate.add(us);
            }
            Update Uslsttoupdate;
            System.debug('Updated count====='+Uslsttoupdate.size());
            // To activate Created Orders/OrderItems
            Database.executeBatch(new QTB_AddOrderProductCSCRBatch(OrdidtoLoad), 200);
            
        }
          catch (exception e) {
            errorIds = e.getMessage() + errorIds;
            if (errorIds.length() > 100000)
                errorIds = errorIds.substring(0, 100000);
            system.debug('In catch method of batch Finish>>>>>>>>>>');
            system.debug('Error inf::' + errorIds);
            QTB_ExceptionLogger.createLog(
                e.getLineNumber(),
                e.getStackTraceString(),
                errorIds,
                e.getTypeName(),
                className
            );
        }  
        
    }
  Public class flowInputs{
        @InvocableVariable
        public Order oldOrderData;
        @InvocableVariable    
        Public string newOrderCreated;
        @InvocableVariable
        Public list<String> productCodes;
      }
    
}
Shubham Jain 338Shubham Jain 338
Hi,

As per error, you are adding a String to the list which is of Type Order

I think the error lies in this statement            
NewCrOrder1.add(flow.newOrderCreated);

Please mark this as the best answer if it helps

Thanks 
Shubham Jain
Ayyagari RameshAyyagari Ramesh
Thanks for your response. Could you please provide the fix?