+ Start a Discussion
mallikharjunarao gundamallikharjunarao gunda 

please write a test class for batchapex calling from trigger

batch apex:
global class BatchApexDemo implements database.batchable<sobject>{
Public string soqlquery;
 Public void setQry(string soqlquery){
    this.soqlquery = 'Select name,status from account limit 1';  
 }
 global database.querylocator start(database.batchableContext bc){
   return database.getquerylocator(soqlquery);
 }
 global void execute(database.batchablecontext bd, list<sObject> sc){
   System.debug('**In Execute Method**');  
 }
 Public void finish(database.batchableContext bc){ 
 }
}
trigger
trigger callbatchapex on Account (after insert) {
 List<account> accList = new List<account>();
 for(account acc : trigger.new){
     if(acc.annualrevenue < 20000)
        accList.add(acc); 
 }
    if(accList.size() > 0)
      database.executebatch(new BatchApexDemo(),200);
}
 
Amit Chaudhary 8Amit Chaudhary 8
Please check below post for Batch job related issue
1) http://amitsalesforce.blogspot.com/2016/02/batch-apex-in-salesforce-test-class-for.html

NOTE:- I found you did not called the setQry method to set the query in your Trigger.
Update your trigger like below
trigger callbatchapex on Account (after insert) {
 List<account> accList = new List<account>();
 for(account acc : trigger.new){
     if(acc.annualrevenue < 20000)
        accList.add(acc); 
 }
    if(accList.size() > 0)
   {
     BatchApexDemo obj = new BatchApexDemo();
     obj.setQry('Select name,status from account limit 1');
      database.executebatch(obj,200);
   }
}
Your Test class like below
@isTest 
public class TriggerTestClass 
{
	static testMethod void testMethod1() 
	{
		Account testAccount = new Account();
		testAccount.Name='Test Account' ;
		testAccount.annualrevenue = 100;
		insert testAccount;

	}
}