3

I have a class that is failing. Sometimes the trigger associated to it is mentioned in the error message. The error messages I receive are listed below. These classes used to run just fine. I'm not sure why they are failing now.

  1. "First error: Update failed. First exception on row 1600 with id 001A000000LNeMrIAL; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AccountOwner: execution of BeforeUpdate caused by: System.LimitException: Too many DML rows: 10001 Trigger.AccountO..."

  2. First error: Apex CPU time limit exceeded.

  3. Message could not be processed and ended up in dead message queue

The class is below:

global public with sharing class AdForce_SRIDTriggerHandler {

private boolean m_isExecuting = false;
private integer BatchSize = 0;
public static boolean isFutureUpdate = false;

public AdForce_SRIDTriggerHandler(boolean isExecuting, integer size){
    m_isExecuting = isExecuting;
    BatchSize = size;
}
@future
public static void OnAfterInsertUpdate(ID [] updatedIDs)
{ 
    List<String> NameList        = new List<String>();
    List<String> MarketList      = new List<String>();
    List<SRID__c> updatedRecords = new List<SRID__c>();
    List<Account> AcctList       = new List<Account>();

    system.debug('AdForce_SRIDTriggerHandler.OnAfterInsertUpdate :: Starting Trigger Handler');

    if(!Test.isRunningTest()){
        for(SRID__c s : [Select id, name, Market__c, User__c From SRID__c Where ID in :updatedIDs])
    {
        if(s.User__c != null)
        {
            updatedRecords.add(s);        // Add SRID to Updated Records
            NameList.add(s.name);
            MarketList.add(s.market__c);
        }
    }
    }else{
        for(SRID__c s : [Select id, name, Market__c, User__c From SRID__c Where ID in :updatedIDs LIMIT 1])
    {
        if(s.User__c != null)
        {
            updatedRecords.add(s);        // Add SRID to Updated Records
            NameList.add(s.name);
            MarketList.add(s.market__c);
        }
    }
    }

    system.debug('AdForce_SRIDTriggerHandler.OnAfterInsertUpdate :: Number of Updated SRIDs ' + updatedRecords.size());
    system.debug('AdForce_SRIDTriggerHandler.OnAfterInsertUpdate :: Querying for Updated SR Accounts');        
    Map<ID, Account> accounts = new Map<ID, Account>(!Test.isRunningTest() ? [Select id, OwnerID, SLS_1__C,Market__C  from Account where SLS_1__C in :NameList And Market__C = :MarketList] : [Select id, OwnerID, SLS_1__C,Market__C  from Account where SLS_1__C in :NameList And Market__C = :MarketList LIMIT 1]);

    system.debug('AdForce_SRIDTriggerHandler.OnAfterInsertUpdate :: Looping Updated Records');
    for(SRID__c s : updatedRecords)
    {
        system.debug('AdForce_SRIDTriggerHandler.OnAfterInsertUpdate :: SRID__c.User__c - ' + s.User__c);
        system.debug('AdForce_SRIDTriggerHandler.OnAfterInsertUpdate :: Looping accounts, looking for SLS_1__c (' + s.Name + '); Market__c (' + s.Market__c + ')');           
        for(id a : accounts.keyset())
        {
            Account Acc1 = accounts.get(a);
            system.debug('AdForce_SRIDTriggerHandler.OnAfterInsertUpdate :: Account (' + a + ';' + Acc1.SLS_1__c  + ';' + Acc1.Market__c + ')');
            if(Acc1.SLS_1__c == s.Name && Acc1.Market__c == s.Market__c)
            {
                system.debug('AdForce_SRIDTriggerHandler.OnAfterInsertUpdate :: Match Found, Setting Accounts Owner to SRID__c.User__c');
                Acc1.Ownerid = s.User__c;
                Acctlist.add(Acc1);
            }
            if(Test.isRunningTest()){
                break;
            }
        }
        if(Test.isRunningTest()){
            break;
        }
    } 
    system.debug('AdForce_SRIDTriggerHandler.OnAfterInsertUpdate :: Updating Account List');
    update Acctlist;
}

public void OnAfterInsert(SRID__c[] newSRIDs)
{
    if ( newSRIDs != null)
    {
        Set<Id> idSet = new Set<Id>();
        for (SRID__c srid : newSRIDs)
        {
            idSet.add(srid.Id);
            if(Test.isRunningTest()){
                break;
            }
        }

        AdForce_IOOwnershipUtil.UpdateOwner(idSet);
    }
}


public void OnAfterUpdate(SRID__c[] oldSRIDs
                            , SRID__c[] updatedSRIDs
                            , Map<ID, SRID__c> SRIDMap){
List<SRID__c>();
        AdForce_IOOwnershipUtil.UpdateOwner(SRIDMap.keySet());                                      
}

public boolean IsTriggerContext{
    get{ return m_isExecuting;}
}

public boolean IsVisualForcePageContext{
    get{ return !IsTriggerContext;}
}

public boolean IsWebServiceContext{
    get{ return !IsTriggerContext;}
}

public boolean IsExecuteAnonymousContext{
    get{ return !IsTriggerContext;}
}
}

This is the trigger:

// Sets the Account Owner based on the first SLS #
trigger AccountOwner on Account (before insert, before update) {
try {
    // Call the security object to handle all security assignments
    AccountSecurity security = new AccountSecurity();
    security.setOwnerId(Trigger.New);

    Map<Id, AccountTeamMember> oldMembers = new Map<Id, AccountTeamMember>([SELECT AccountId, TeamMemberRole, UserId FROM AccountTeamMember WHERE (AccountId in :Trigger.NewMap.keySet()) AND (TeamMemberRole LIKE 'SLS%')]);

    // Call the security object to handle all security assignments
    List<AccountTeamMember> members = security.getPartners(Trigger.New);
    List<AccountTeamMember> newMembers = new List<AccountTeamMember>();

    for (AccountTeamMember member : members) {
        if (!oldMembers.containsKey(member.Id))
            newMembers.add(member);
    }

    delete oldMembers.values();

    if (!newMembers.isEmpty())
        insert newMembers;
}
catch (Exception e) {
    System.Debug(LoggingLevel.ERROR, 'Unable to update account owner');
    System.Debug(LoggingLevel.ERROR, e);
}
}
2
  • 3
    I've edited you post to remove the many lines of code that had been commented out. In the future, please remove commented code for the sake of readability for those who might be trying to help. It can be very confusing and often discourages those who might otherwise be willing to provide assistance.
    – crmprogdev
    Mar 11, 2015 at 4:30
  • Seeing as your errors indicate that too many rows are being processed (over the DML limit, and CPU Time exceeded), I would add a debug statement after each query to determine how many rows are being pulled, and whether this number is excessive. Mar 11, 2015 at 14:52

1 Answer 1

1
  1. Acctlist has records more than 10000(SOQL Limit for DML) put a check before update statement to avoid this exception.

  2. for CPU time limit errors, identify nested for loops and try to avoid them for example using Maps.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .