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
BibhuBibhu 

How to optimize trigger showing error "

Hi 

I am getting an error in our production environment , whenever a BXUK_Key_Account_Management__c record is created.

"Too many DML rows"  , I have pasted the code below. Kindly show me the way to efficiently rewrite the code. Appreciate your help.

trigger TGR_Push_Classification_at_Account_Level on BXUK_Key_Account_Management__c (after Update, after Insert) 
{
    List<Account> accList = new List<Account>();
    Map<id,String> kamBSMap = new Map<id,String>();
    Map<id,String> kamHCMap = new Map<id,String>();
    Set<id> accIdSet = new Set<id>();
    Map<String,String> classificationMap = new Map<String,String>();
    classificationMap.put('Key Account', 'A');
    classificationMap.put('Key Development Account', 'B');
    classificationMap.put('Maintenance Account', 'C');
    classificationMap.put('Standard Account', 'D');
    
    
    for(BXUK_Key_Account_Management__c kam : Trigger.new)
    {
        accIdSet.add(kam.BXUK_Account__c);
        
        if(kam.BXUK_Business_Unit__c != null && !''.equals(kam.BXUK_Business_Unit__c))
        {
            if(kam.BXUK_Business_Unit__c == 'Biosurgery')
            {
                kamBSMap.put(kam.BXUK_Account__c,classificationMap.get(kam.BXEU_Account_Classification__c));
            }
            if(kam.BXUK_Business_Unit__c == 'Hospital Care')
            {
                kamHCMap.put(kam.BXUK_Account__c,classificationMap.get(kam.BXEU_Account_Classification__c));
            }

        }
    }
    Account[] departments = [select id from Account where ParentId in : accIdSet OR parent.ParentId in : accIdSet];
    
    for(Account dept: departments)
    {
    accIdSet.add(dept.Id);
    }

    Account[] accs = [select id,ParentId,Parent.ParentId, BXEU_Biosurgery_Classification__c, BXEU_Hospital_Care_Classification__c from Account where id in:accIdSet];
    
    
    for(Account acc : accs)
    {
       if(kamBSMap.containsKey(acc.Id))
       {
          acc.BXEU_Biosurgery_Classification__c = kamBSMap.get(acc.Id);
       }
       else if(kamBSMap.containsKey(acc.ParentId))
       {
          acc.BXEU_Biosurgery_Classification__c = kamBSMap.get(acc.ParentId);
       }
       else if(kamBSMap.containsKey(acc.Parent.ParentId))
       {
          acc.BXEU_Biosurgery_Classification__c = kamBSMap.get(acc.Parent.ParentId);
       }

       if(kamHCMap.containsKey(acc.Id))
       {
            acc.BXEU_Hospital_Care_Classification__c = kamHCMap.get(acc.Id);
       }
       else if(kamHCMap.containsKey(acc.ParentId))
       {
          acc.BXEU_Hospital_Care_Classification__c = kamHCMap.get(acc.ParentId);
       }
       else if(kamHCMap.containsKey(acc.Parent.ParentId))
       {
          acc.BXEU_Hospital_Care_Classification__c = kamHCMap.get(acc.Parent.ParentId);
       }
    
       accList.add(acc);
    }

    try        
    {
    if(accList.size()>0){
    update accList;
    }
    }
    catch(Exception e)       
    {        
    System.debug(e.getMessage());        
    }

 }

 

Thanks

 

Bibhu

 

Shailesh DeshpandeShailesh Deshpande

Hi Bibhu,

 

I believe the error is coming because there are 2 queries in your code and the amount of data returned by them is large...so 1 alternative for you is to use shift the logic to batch apex after you have formed the set "accSetId".

 

 

Thanks,

Shailesh. P. Deshpande