0

I have a class that is failing OnAfterInsertUpdate. I'm getting the following errors: First error: Apex CPU time limit exceeded and "First error: Update failed. First exception on row 1200 with id 001A000000BpQFaIAN; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AccountOwner: execution of BeforeUpdate caused by:

System.LimitException: Too many DML rows: 10001 Trigger.AccountO...".

I found these errors in a query I pulled from Workbench. 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');
    for(SRID__c s : !Test.isRunningTest() ? [Select id, name, Market__c, User__c From SRID__c Where ID in :updatedIDs] : [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 OnBeforeInsert(SRID__c[] newSRIDs)
{
}
*/


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);
    }
}


/*
@future public static void OnAfterInsertAsync(Set<ID> newSRIDIDs){
}
*/

public void OnAfterUpdate(SRID__c[] oldSRIDs
                            , SRID__c[] updatedSRIDs
                            , Map<ID, SRID__c> SRIDMap){
    //List<SRID__c> sridUserChanged = null;
    //for (Integer i = 0; i < BatchSize; i++)
    //{
    //  if ( oldSRIDs[i].User__c != updatedSRIDs[i].User__c)
    //  {
    //      if ( sridUserChanged == null ) sridUserChanged = new List<SRID__c>();
    //      sridUserChanged.add(updatedSRIDs[i]);
    //  } 
    //}
       AdForce_IOOwnershipUtil.UpdateOwner(SRIDMap.keySet());                                      
}

/*
public void OnBeforeUpdate(SRID__c[] oldIOs
                        , SRID__c[] updatedIOs
                        , Map<ID, SRID__c> IOMap){  
}
*/

/*
@future public static void OnAfterUpdateAsync(Set<ID> updatedSRIDIDs)
{
} 
*/

/*
public void OnBeforeDelete(SRID__c[] SRIDsToDelete
                            , Map<ID, SRID__c> SRIDIDMap){
}
*/

/*
public void OnAfterDelete(SRID__c[] deletedSRIDs
                        , Map<ID, SRID__c> SRIDIDMap){
}
*/

/*
@future public static void OnAfterDeleteAsync(Set<ID> deletedSRIDIDs){
}
*/

/*
public void OnUndelete(SRID__c[] restoredSRIDs){
}
*/

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;}
}
}

The class below is a dependency of the class above: public with sharing class AdForce_IOOwnershipUtil {

@future 
public static void UpdateOwner(Set<Id> updatedSridIds)
{
if (updatedSridIds == null) return;
List<Orders__c> orders = [SELECT 
              Id
              , OwnerId
              , o.Sales_Rep_ID__c 
              FROM Orders__c o 
              WHERE o.Sales_Rep_ID__c IN: updatedSridIds];
if ( orders.size() > 0)
{
  List<SRID__c> sridChanges = AdForce_SridDAO.getInstance().getById(updatedSridIds);
  for (SRID__c srid : sridChanges)
  {
    for (Orders__c ord : orders)
    {
      if (ord.Sales_Rep_ID__c == srid.ID)
        ord.OwnerID = srid.User__c;
    }
  }
  AdForce_IOTriggerHandler.isFutureUpdateIO = false;
  try
  {
    update orders;
  }
  catch (Exception e)
  {
    //TO DO: logg and email error;
  }
}

}
}
  • the error hints at the update AcctList statement passing in too many values as the error appears to be on an Account trigger. Assuming this is a legitimate use case, you may need to update the accounts using a batch class which won't be bound by the 10000 limit – cropredy Feb 14 '15 at 1:30
1

The error means that one of your queries is trying to return too many rows. Salesforce has a limit on the number of rows that can be returned in one context.

The rest of that error message seems to be cut off, but I can only see a few queries there. It's possible that it's the query on Orders since that isn't bound by the number of incoming Ids. It's feasible that cumulative number of Salespeople have more than 10k orders.

If you have the full error it should help debug further. Most likely you'll need to limit the number being returned or refactor around the issue. Depends on which query is failing.

  • Thank you for the response! Which line are you referring to? – Blair Kristin Feb 23 '15 at 16:20
  • I don't see line numbers, so this line:List<Orders__c> orders = [SELECT Id , OwnerId , o.Sales_Rep_ID__c FROM Orders__c o WHERE o.Sales_Rep_ID__c IN: updatedSridIds]; – IamTheFij Feb 24 '15 at 18:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.