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
VishwanathVishwanath 

'System.LimitException: Too many SOQL queries: 101'

Hi,

 

How to avoid 'System.LimitException: Too many SOQL queries: 101'

 

if(Trigger.isUpdate || Trigger.isUpdate)
     {
     Double ReportOverage;
    Double diffOverage;
    Double AuditOverage;
    for(UOP_Royalty_Payment_History__c UOP:Trigger.new)
    {
        if(UOP.Payment_History_Event__c!='Report')
        {
            List<UOP_Royalty_Payment_History__c> UOPList=[select id,name,Payment_History_Event__c,Overage__c,Throughput__c,Previous_Fully_Paid_Capacity__c from UOP_Royalty_Payment_History__c where Payment_History_Event__c=:'Report' and Process_Unit__c=:UOP.Process_Unit__c and UOP_Year__c=:UOP.UOP_Year__c limit 1];
            if(UOPList.size()>0)
            {
                ReportOverage=UOPList[0].Overage__c;
            }
            if(ReportOverage==null) // || ReportOverage=='')
            {
               UOP.Invoice_Ammount_For_FA_DA__c=UOP.Invoice_Amount__c;                 // Trigger.new[0].UOP_Year__c.addError('There is no Report in '+UOP.UOP_Year__c+'');  
            }
            else
            {
                AuditOverage=UOP.Throughput__c - UOP.Previous_Fully_Paid_Capacity__c;
                diffOverage=AuditOverage - ReportOverage;
                if(diffOverage <= 0)
                {
                    UOP.Invoice_Ammount_For_FA_DA__c=0.0;
                }
                else
                {
                    UOP.Invoice_Ammount_For_FA_DA__c=diffOverage * UOP.Royalty_Rate__c * (UOP.Applicable_BLS11__c / UOP.BLS_Base__c);
                }
            }
        }
        }
     }

 thansk

bob_buzzardbob_buzzard

You have the classic issue of a SOQL query inside the for loop that is iterating the trigger.  You really need to retrieve all UOP_Royalty_Payment_History__c records that you are interested in in one go. 

VishwanathVishwanath

hi   bob buzzard,

 

 

Im tring to retrive the only one overage value from the list depending on the conditions, how come i solve this issue,

 

bob_buzzardbob_buzzard

You are only retreiving one, but doing this for every record in the trigger.  Thus when your trigger has > 100 records, you hit this problem.

 

I think you'll have to restructure your query - you may need to pull back all records that match any of the values from the trigger records and then post-process the query results reduce it to a single match per trigger record.

Chamil MadusankaChamil Madusanka

As Bob said, you have hit common issues. Refere following link to get understand on best practice of wrting triggers

 

http://wiki.developerforce.com/page/Apex_Code_Best_Practices

 

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.