+ Start a Discussion
Venkata Sravan Kumar BandariVenkata Sravan Kumar Bandari 

can i call batch apex method from controller class...if yes then how.....?

Ankit AroraAnkit Arora
If I din't misunderstood the question then you want to execute the batch class in your controller apex class right?

If yes then what is the problem using this :

BatchClassName bcn = new BatchClassName() ;
ID batchprocessid = Database.executeBatch(bcn);

Venkata Sravan Kumar BandariVenkata Sravan Kumar Bandari
K Thank U.........but in test class how it is possible...?
Maimoona S.Maimoona S.
for test class here is the sample code;

List<sObject> xlist=[select id from ..........];
 database.batchablecontext Obj;
 BatchClass xbatch=new BatchClass();

Hope this helps!
Grazitti TeamGrazitti Team

Please follow the below points which helps you to resolve the issue:

Batch class:

 Should write code in the batch class which handles both scenario(test code and normal execution) . Please see the code below for refrence.
Global Database.QueryLocator start(Database.BatchableContext BC){
        String query='';
             query = 'Select id From User  LIMIT 1';    
            query = 'Select id From User ';   
        return Database.getQueryLocator(query);

Test Class:

1. Create the dummy data which will be used in the batch class.
2. Create a instance of the batch class.

Please see the below code:

Id p = [select id from profile where name='System Administrator'].id;
        Account ac = new Account(name ='Grazitti') ;
        insert ac;  
        //Non-egencia select user
        Contact con = new Contact(LastName ='testCon',AccountId = ac.Id);
        insert con;   
        User user = new User(alias = 'test123', email='test123@noemail.com',
                emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
                localesidkey='en_US', profileid = p, country='United States',IsActive =true,
                ContactId = con.Id, timezonesidkey='America/Los_Angeles', username='tester@noemail.com');
        insert user;

        [batchClassName] b1 = new [BatchClassName]();
         ID batchprocessid1 = Database.executeBatch(b1,10);

Please mark as best answer if it solves your issue.

Grazitti Team,

Venkata Sravan Kumar BandariVenkata Sravan Kumar Bandari
        Database.BatchableContext dbc;
        BAClass batch_obj = new BAClass();

        ID batchprocessid = Database.executeBatch(batch_obj);

getting error : Method does not exist or incorrect signature: test.startTest()

without writing test.startTest() and test.stopTest() methods............

Here the final method is calling two times but while calling executebatch the start & execute methods are not executing......
Venkata Sravan Kumar BandariVenkata Sravan Kumar Bandari
what is wrong with this code nothing updated in sobject but program successfully executed

public class BAClass implements Database.Batchable<name__c>{
    public Iterable<name__c> start(Database.BatchableContext bc) {
        System.debug('START METHOD');
        list<name__c> name_list = new list<name__c>();
        name_list = [select id,name,qualification__c, student_id__c, email__c from name__c];
        return name_list;
    public void execute( Database.BatchableContext bc, list<name__c> l) {
        System.debug('EXECUTE METHOD');
        decimal i =1000;
        for ( name__c n : l){
            n.student_id__c = i;
        update l;
    public void finish(Database.BatchableContext bc) {
        System.debug('Batch Apex program successfully executed..');

public class BAClass_text {
    static public testmethod void Batch() {
        list<name__c> l = new list<name__c>();
        Database.BatchableContext dbc;
        BAClass batch_obj = new BAClass();
        batch_obj.execute(dbc, l);
        //ID batchprocessid = Database.executeBatch(batch_obj);