+ Start a Discussion
Vinothini murugesh 10Vinothini murugesh 10 

System.AsyncException: Database.executeBatch cannot be called from a batch or future method. kindly hep me


global class Batch_CustomerCategoryUpdate implements Database.Batchable<sObject>{
    public Static Set<Id> setAccountIds=new Set<Id>();
    public Static Map<Id,String> mapString=new  Map<Id,String>();
    public Static String categories;
    public set<Id> accIds;
    public Static boolean skipAccountTrigger=false;
    public Static List<Account> accUpdateList=new List<Account>();
    public Batch_CustomerCategoryUpdate(Set<Id> tSetAccId){
        // Get all the child account Ids
        //getAllChildAccountIds(tsetAccId);
        accIds=tsetAccId;
    }
    global Database.Querylocator Start(Database.batchableContext bc){
        
        String strQuery='Select Id, Name, Customer_Category__c,Customer_Categories_for_MC__c,Primary_Category_for_MC__c from Account where id in :accIds';
        system.debug('size od ids'+accIds.size());
        system.debug(''+strQuery);
        return Database.getQueryLocator(strQuery);
    }
    global void execute(Database.batchableContext bc,List<sObject> sObjList){
        System.debug(''+setAccountIds.size()+' -setAccounIds- '+setAccountIds);
        List<Account> accList=(List<Account>)sObjList;        
        if(accList[0].Customer_Categories_for_MC__c!=null){
            Set<Id> setIds=new Set<Id>();
            setIds.add(accList[0].Id);
            getAllChildAccountIds(setIds);
            for(Account acc:accUpdateList){
                acc.Customer_Categories_for_MC__c = accList[0].Customer_Categories_for_MC__c;
                acc.Customer_Category__c = accList[0].Customer_Category__c;
                if(accList[0].Customer_Categories_for_MC__c.contains(';')){
                
                acc.Primary_Category_for_MC__c = accList[0].Customer_Categories_for_MC__c.split(';')[0];
                }
              
            }
           utility.IsAccountAfterUpdateCalled=true;
            update accUpdateList;
        }
    }
    global void finish(Database.batchableContext bc){
        //Nothing
    }
    
    // Return list of all child Account Ids
    public static void getAllChildAccountIds(set<Id> setAccId){
        Set<Id> tempIdSet=new Set<Id>();
        for(Account acc:[Select Id, name, Customer_Categories_for_MC__c,Customer_Category__c,inherit__c,Primary_Category_for_MC__c from Account where parentid in :setAccId  and Synchronize__c=true ]){
            accUpdateList.add(acc);
            //for(Account ownr:acc.Accounts_OwnerGroup__r){
               tempIdSet.add(acc.Id);
            //}
            //for(Account topn:acc.Accounts_TopNode__r){
            //}
            //for(Account sold:acc.SoldTo__r){
            //}
        }
        if(tempIdSet.size()>0){
            setAccountIds.addAll(tempIdSet);
            getAllChildAccountIds(tempIdSet);
        }
    }
}
v varaprasadv varaprasad
Hi Vinothini,
You are calling batch apex in finish method or any other method.
The future method will not support for the batch apex.

Please check once Apex batch class Minimum version should be 27.0.To call another batch in finish method.


Hope this helps you!

Thanks
Varaprasad
For Support : varaprasad4sfdc@gmail.com




 
Vinothini murugesh 10Vinothini murugesh 10
 if(!accIds.isEmpty()){
            
            if(accIds.size()==1){
                
                getAllChildAccountIds(accIds);
                System.debug('*** accUpdatenewList size: ' +accUpdatenewList.size());              
                for(Account acc:accUpdatenewList){
                    
                    if(accRef.Customer_Categories_for_MC__c != null){
                        
                        acc.Customer_Categories_for_MC__c = accRef.Customer_Categories_for_MC__c;
                        //  if(acc.inherit__c == true){
                        acc.Customer_Category__c = accRef.Customer_Category__c;
                        acc.Primary_Category_for_MC__c = accRef.Customer_Categories_for_MC__c.split(';')[0];
                        //  }
                        acc.IgnoreValidation__c = true;
                    }
                }
                
                if(accUpdatenewList.size() > 0){
                    utility.IsAccountAfterupdateCalled=true;
                    toBeExecuted = false;//***RG
                    System.debug('toBeExecuted: ' +toBeExecuted);
                    //  update accUpdatenewList;
                    if(accUpdatenewList.size() < 100){ //determine the best number to use based on limits with enough buffer
                       system.debug('inside batch'+accUpdatenewList.size());
                        update accUpdatenewList;  
                    }else{
                        if([SELECT count() FROM AsyncApexJob WHERE JobType='BatchApex' AND Status in ('Holding','Queued','Preparing','Processing')] < 100){            
                             system.debug('inside batch'+accUpdatenewList.size());
                            categorySelectionBatch batch1 = new categorySelectionBatch(accUpdatenewList);
                            Id batch1Id = Database.executeBatch(batch1);
                        }else{
                            Account acct = new Account();
                            acct.addError('There are more than 100 batches of Apex code scheduled. Please try again after some time and if the issue repeats, log a case with LST Connect team');//Use a Label
                        }
                    }
                }
            }
            
            else{
              Database.executeBatch( new Batch_CustomerCategoryUpdate(accIds), (Test.isRunningTest() ? accIds.size() : 1) );
            }


Herre we are calling like this
v varaprasadv varaprasad
Hi Vinothini,

if(!accIds.isEmpty()){
            
            if(accIds.size()==1){
                
                getAllChildAccountIds(accIds);
                System.debug('*** accUpdatenewList size: ' +accUpdatenewList.size());              
                for(Account acc:accUpdatenewList){
                    
          above code is another batch apex finish method or execute method please confirm.

If it is in finish method.
The minimum version should be 27.0.To call another batch in finish method.

User-added image


Hope this helps you!

Thanks
Varaprasad
@For Support : varaprasad4sfdc@gmail.com
 
Vinothini murugesh 10Vinothini murugesh 10
prasad,

the code os fr calling anoth batch class.

     
            if(accIds.size()==1){
                
                getAllChildAccountIds(accIds);
                System.debug('*** accUpdatenewList size: ' +accUpdatenewList.size());              
                for(Account acc:accUpdatenewList){
                    
                    if(accRef.Customer_Categories_for_MC__c != null){
                        
                        acc.Customer_Categories_for_MC__c = accRef.Customer_Categories_for_MC__c;
                        //  if(acc.inherit__c == true){
                        acc.Customer_Category__c = accRef.Customer_Category__c;
                        acc.Primary_Category_for_MC__c = accRef.Customer_Categories_for_MC__c.split(';')[0];
                        //  }
                        acc.IgnoreValidation__c = true;
                    }
                }
                
                if(accUpdatenewList.size() > 0){
                    utility.IsAccountAfterupdateCalled=true;
                    toBeExecuted = false;//***RG
                    System.debug('toBeExecuted: ' +toBeExecuted);
                    //  update accUpdatenewList;
                    if(accUpdatenewList.size() < 100){ //determine the best number to use based on limits with enough buffer
                       system.debug('inside batch'+accUpdatenewList.size());
                        update accUpdatenewList;  
                    }else{
                        if([SELECT count() FROM AsyncApexJob WHERE JobType='BatchApex' AND Status in ('Holding','Queued','Preparing','Processing')] < 100){            
                             system.debug('inside batch'+accUpdatenewList.size());
                            categorySelectionBatch batch1 = new categorySelectionBatch(accUpdatenewList);
                            Id batch1Id = Database.executeBatch(batch1);

                        }.

The version i have changed to 40