1

I need to run Account Assignment rules after Account is saved to DB and update Account owner and add Account team members later. During batch update, it's erroring which is logical. How can I fix it?

Trigger:

trigger AccountTriggers on Account (after insert, after update) 
{
List<Id> accountIdList=new List<Id>();

    for (Account a : Trigger.new) 
    {   
        if(Trigger.isAfter)
        {   accountIdList.add(a.Id);  }
    }
    if(accountIdList.size() > 0)
        Database.executeBatch(new BatchJobForAccountAssignmentTrigger(accountIdList));
}

Class:

global class BatchJobForAccountAssignmentTrigger implements Database.Batchable<sObject>{

   Account temp;
   List<Account> updateAccountLs=new List<Account>();
   List<AccountTeamMember> DeleteatmList=new List<AccountTeamMember>();
   List<AccountTeamMember> atmList = new List<AccountTeamMember>();
   List<Account> ARLs = new List<Account>();

   AccountTeamMember atmTemp;

   String query='Select Account_Status__c, Region__c, ownerId,billingCountry,BillingState,BillingStateCode,Secondary_Library_Type__c,is_Strategic_Account__c,Type,Primary_Library_Type__c, recordtypeid from Account where Id in: accountIdList and Skip_Assignment__c != TRUE and recordtypeid=\'012j0000000tdRx\'';   

   List<Account_Assignment__c> aaList;
   List<Id> accountIdList = new List<Id>();


   global BatchJobForAccountAssignmentTrigger(List<Id> accountLs)
   {
       aaList =  [SELECT Account_Status__c, Regions__c, State__c,Country__c,Is_Strategic_Account__c,Library_Types__c,Account_Types__c,Member_Services_Rep__c,Inside_Sales_Rep__c, Account_Owner__c FROM Account_Assignment__c];    

       if(accountLs!=null){
           accountIdList=accountLs;
       }
   }

   global Database.QueryLocator start(Database.BatchableContext BC){
      //System.debug('fire');
      return Database.getQueryLocator(query);
   }

   global void execute(Database.BatchableContext BC, List<sObject> ls)
   {

//Doing some validations
.
.
//Adding Accounts to be updated into a list
.//Adding Team members to be added into a list
.
.
if(updateAccountLs.size()>0){
         list<list<Account>> updateAccountFullLs=Utils.getSubListForAccount(updateAccountLs);
         for(list<Account> tempSubAccountLs: updateAccountFullLS){
             database.update(tempSubAccountLs);
             system.debug('ACOUNTSSS='+tempSubAccountLs);
         }   

     }
 if(atmList.size()>0){
         list<list<AccountTeamMember>> insertAccountTeamMemberFullLs=Utils.getSubListForAccountTeamMember(atmList);
         for(list<AccountTeamMember> tempSubAtmLs: insertAccountTeamMemberFullLs){
             database.insert(tempSubAtmLs);
         }
 global void finish(Database.BatchableContext BC)
   {}
4

Simple, you can add this conditional: !system.isBatch()

if(!system.isBatch() && accountIdList.size() > 0)
    Database.executeBatch(new BatchJobForAccountAssignmentTrigger(accountIdList));
}
  • OMG, this made my Friday very easy.!! Thanks a ton.!! – Amulya Nov 2 '18 at 19:18
  • I'm not sure that isBatch is honored in testmethods – cropredy Nov 2 '18 at 19:42
  • Pretty sure it is...will have to investigate. – Adrian Larson Nov 2 '18 at 19:57
  • isBatch is honored in Testmethods. Just verified. – Amulya Nov 5 '18 at 17:02

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.