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
Anshul Kapoor 13Anshul Kapoor 13 

I am processing 2 records, but 10 soql queries & 5 dml statements are processed. How?

Hi All,

My issue is: In my code, only 2 SOQL's are processing, but I am getting 6 SOQL is processed as per debug log. Could somebody tell me, why I am getting these many SOQL's are processed.

Below is my code:

Apex Trigger:

Trigger AccountTrigger on Account (after update){
    if (StopRecurssion.OnetimeRun5 && Trigger.isUpdate && Trigger.isAfter){ //after update
        AccountTriggerHandler ATH = new AccountTriggerHandler();
        ATH.BillingEventAccountUpdate(Trigger.oldMap, Trigger.newMap); //Calls method on change of 'Account Owner' 
        StopRecurssion.OnetimeRun5 = false;
    }
}

Class:

 public class AccountTriggerHandler{
    public void BillingEventAccountUpdate(Map<Id, Account> oldMapAcnt, Map<Id, Account> newMapAcnt){
        List<Billing_Event__c> lstBE = new List<Billing_Event__c>(); //List of Billing Events
        Set<Id> setAcntId = new Set<Id>(); //Set of Account Ids
        Map<Id, Id> mapDealId_AcntOwnerId = new Map<Id, Id>(); //Map of 'Deal' Id & 'Account-OwnerId'
        
        for (Account acc : newMapAcnt.values()){ //Get the Set of 'AccountIds'
            if (oldMapAcnt.get(acc.Id).OwnerId != newMapAcnt.get(acc.Id).OwnerId){
                setAcntId.add(acc.Id);
            }
        }
        
        if (setAcntId.size() > 0){ //Get the Map of 'DealId' & 'Account-OwnerId'
            for (Deal__c deal : [select Id, Vendor__r.OwnerId from Deal__c where Deal_Status__c = 'Active' AND Event_Total__c > 0 AND Vendor__c IN :setAcntId]){
                system.debug('dealId** ' + deal.Id);
                mapDealId_AcntOwnerId.put(deal.Id, deal.Vendor__r.OwnerId);
            }
        }   
        
        //Update the 'Account-Owner' to all the corresponding 'Billing Events Owners'
        if (mapDealId_AcntOwnerId.size() > 0){ 
            for (Billing_Event__c be : [select Id, Deal_1__c, OwnerId from Billing_Event__c where Deal_1__c IN :mapDealId_AcntOwnerId.keySet() AND OwnerId != :mapDealId_AcntOwnerId.values()[0] LIMIT 2]){
                system.debug('beId** ' + be.Id);
                be.OwnerId = mapDealId_AcntOwnerId.get(be.Deal_1__c);
                lstBE.add(be);
            }
        }
        if (lstBE.size() > 0){
            system.debug('Size** ' + lstBE.size());
                    update lstBE;
        }
    }
}
Anshul Kapoor 13Anshul Kapoor 13
There is no package installed in the system. No Trigger is processing in the backend. For one 'Deal' & 'Billing Event' Record, I am getting the below Debug Log:

 Number of SOQL queries: 6 out of 100
  Number of query rows: 6 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 5 out of 150
  Number of DML rows: 5 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

There should be 2 SOQL queries & 1 DML statement should be performed. But, in the above debug log, the count is 6 & 5 for SOQL & DML statement respectively