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
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();
 
xbatch.start(Obj);
xbatch.execute(Obj,xlist);
xbatch.finish(Obj);



Hope this helps!
Grazitti TeamGrazitti Team
Hi,

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='';
        if(test.isrunningtest()){
             query = 'Select id From User  LIMIT 1';    
        }else{
            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.

Regards,
Grazitti Team,
www.grazitti.com


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

        ID batchprocessid = Database.executeBatch(batch_obj);
 test.stopTest();


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;
            i+=1;
        }
        update l;
    }
   
    public void finish(Database.BatchableContext bc) {
        System.debug('Batch Apex program successfully executed..');
    }
}


@isTest
public class BAClass_text {
    static public testmethod void Batch() {
        //test.startTest();
        list<name__c> l = new list<name__c>();
        Database.BatchableContext dbc;
        BAClass batch_obj = new BAClass();
        batch_obj.start(dbc);
        batch_obj.execute(dbc, l);
        batch_obj.finish(dbc);
        //ID batchprocessid = Database.executeBatch(batch_obj);
        //test.stopTest();
    }
}