+ Start a Discussion
Ajith Selvaraj 2Ajith Selvaraj 2 

Reason to getting an error like "System.LimitException: Too many SOQL queries: 101"

I have more than 500 records in invoice which is in related list to one account and the name of the account is "Account1"(Invoice- Account: Lookup relationship). Now Im try to insert more than 10 records into Invoice for the same account using dataloader. In that time only Im getting an error like "System.LimitException: Too many SOQL queries: 101". 
And my code is public class CommercientSF21InvoiceTriggerHandler1 {
    public static void accountUpdate(List<Invoice__c> currentRecordId){
        List<Account> accUpdate = new List<Account>();
        List<Account> accountWithoutInvoice = new List<Account>();
        Id accountId;
        for(Invoice__c acc : currentRecordId ){
            accountId = acc.Account__c;
        List<Invoice__c> invoiceList = [select Id,CommercientSF21_IVM_InvoiceDate__c from Invoice__c where Account__c=: accountId];
         for(Account acc : [select id,Current_Year_Revenue__c,Last_Order_Date__c,
                           Previous_Year_Revenue__c from Account where Id =:accountId]){
                               if(invoiceList.size() == 0){
                                   acc.Current_Year_Revenue__c = NULL;
                                   acc.Lifetime_Value_LTV__c = NULL;
                                   acc.Previous_Year_Revenue__c = NULL;
        map<id,account> accmap = new map<id,account>();
            update accmap.values();
        Set<Id> currentYearRecordIds = new Set<Id>();
        Set<Id> previousYearRecordIds = new Set<Id>();
        for(integer i=0; i<invoiceList.size(); i++){ 
            Datetime record = invoiceList[i].CommercientSF21_IVM_InvoiceDate__c;
            if(record != NULL){ 
                String yearoftherecord = String.valueOf(record.Year());
                Integer curentYear = Date.Today().Year();
                String currentYearInteger = String.valueOf(curentYear);
                Integer previousyear = curentYear -1;
                String previousyearInteger = String.valueOf(previousyear);
                if(currentYearInteger == yearoftherecord ){
                else if(previousyearInteger == yearoftherecord){
        List<AggregateResult> currentYearAmountList = [SELECT SUM(CommercientSF21_IVM_InvoiceAmt_c__c )tot FROM Invoice__c WHERE Id= :currentYearRecordIds];
        List<AggregateResult> previousYearAmountList = [SELECT SUM(CommercientSF21_IVM_InvoiceAmt_c__c )tot FROM Invoice__c WHERE Id= :previousYearRecordIds];
        List<AggregateResult> totalAmountList = [SELECT SUM(CommercientSF21_IVM_InvoiceAmt_c__c )tot FROM Invoice__c WHERE Account__c =: accountId ];
        Object sumAmountCurrentYear;
        Object sumAmountPreviousYear; 
        Object sumAmountTotalYear;
        for(AggregateResult acc : currentYearAmountList){
            sumAmountCurrentYear = acc.get('tot');  
        for(AggregateResult acc : previousYearAmountList){
            sumAmountPreviousYear = acc.get('tot');  
        for(AggregateResult acc : totalAmountList){
            sumAmountTotalYear = acc.get('tot');  
        for(Account acc: [select id,Current_Year_Revenue__c,Last_Order_Date__c,
                          Previous_Year_Revenue__c from Account where Id =: accountId] ){
                              acc.Previous_Year_Revenue__c = Integer.valueOf(sumAmountPreviousYear);
                              acc.Current_Year_Revenue__c = Integer.valueOf(sumAmountCurrentYear);
                              acc.Lifetime_Value_LTV__c = Integer.valueOf(sumAmountTotalYear);
        map<id,account> accMapToUpdate = new map<id,account>();
            update accMapToUpdate.values();
Brian Nolau 2Brian Nolau 2
Governor limits – 100 queries in a single transaction duh
mrferdos mrferdosmrferdos mrferdos
I like the valuable info in your articles. I'll bookmark your blog and check again here regularly.www.office.com/setup I'll learn plenty of new stuff right here! Best of luck for the next! Regards.Office.com/setup