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
ashiiashii 

Need help (Batch Apex from trigger) - Test class for Batch Apex which is being called from Trigger

hi all,

 

 

 

I have a scenario, please find the details below:

 

 

 

If a user record "insert" then I'll call a "trigger" (Say T1) on "after insert".

 

In trigger T1 I'll call a global class (say GC1), from this global class I'll call a Batch Apex class (say BAC1).

 

I have already put a limit to my batch (size = 50).

 

Because I have seen in forum that we can execute only 200 records in test class. So anyway this scenario is covered with my batch size limit , which is only 50.

 

 

 

eg: Database.execute(BAC1,50)

 

 

 

But then also I am getting below error

 

------------------------------------------------------

 

System.UnexpectedException: No more than one executeBatch can be called from within a testmethod. Please make sure the iterable returned from your start method matches the batch size, resulting in one executeBatch invocation.

 

------------------------------------------------------

 

  

 

Please note that, I have already placed a global variable which will stop the recursive trigger. Otherwise you might be thinking when I update the user record it will call the trigger again and it will go to a recursive call. This is already handled using a global variable. The global variable will be NULL first time, after that it will set to TRUE. So the trigger execute only once if global variable is NULL.

 

  

 

  

 

Hope some body can help me out to solve this issue.

 

  

 

Trigger Code

 

------------

 

 trigger RollupValues on User (after insert,after update) {
    if(RollupValues.IsNoTrigger == null)
    {
        List<Profile> WaterProf = [Select Id from Profile where name = 'Profile Sales'];
        for(User us : Trigger.New)
        {
            //**** Trigger only if the profile is "Profile Sales"
            if(us.ProfileId == WaterProf[0].Id)
            {
                RollupValues.UpdateRollupValues(Trigger.New);   
            }   
        }   
    }       
}

 

---------------

 

  

 

Global Class Code

 

---------------

 

  Global class RollupValues
{
    public static Boolean IsNoTrigger{get;set;}
    //public  {get;set;}
    public static void UpdateRollupValues(User[] users)
    {
      
        //**** Check the global variable to avoid recursive trigger
        system.debug('Global value:'+RollupValues.IsNoTrigger);       
        if(RollupValues.IsNoTrigger == null)
        {
            //**** Call batch update class to update the Rollupvalues
            string bquery = 'Select id,userroleId,ManagerId from User where Profile.Name = '+ '\'' + 'Profile Sales' + '\'';
  //**** Call Batch apex class
            BatchRollupValues objbatchupdate = new BatchRollupValues();
            objbatchupdate.query = bquery;
            Database.executeBatch(objbatchupdate,50);
 }
     }
}

---------------

 

  

 

Thanks in advance!

 

  

 

ashii

 

 

 

kritinkritin

use some dynamic limiting for you global class. means limit 100 or 200 in your query.

 

declare one global variable like: 

public

 

 

String limitSize;string bquery = 'Select id,userroleId,ManagerId from User where Profile.Name = '+ '\'' + 'Profile Sales' + '\'';
  (limitSize!=null)' LIMIT ' + limitSize;

 

if

{

bquery= bquery +

}

 

and you can set this limit values in your testclass setting values.

 

Thanks

ashiiashii

Hi Kritin,

 

Thanks for your suggestion. My doubt is like, I have already put a batch limit of size '50'. So do I need to again filter in the query also?

 

Thanks

ashii