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
Krishna B 86Krishna B 86 

How to call from apex class method to batch methods.

I have apex class and methods. How to call from apex class method into batch apex class start method. If any one could you please suggest me.
ANUTEJANUTEJ (Salesforce Developers) 
Hi Krishna,

I have tried executing a simple method with a debug statement in a class from the execute method in the batch class and I saw that I was able to get statement in the debug logs.

I think you should be able to call the apex class methods normally.

I hope this helps, in case if this comes handy can you please choose this as best answer so that it can be used by others in the future.

Regards,
Anutej.
Krishna B 86Krishna B 86
I need apex class method into start method. could you please suggest me.
Footprints on the MoonFootprints on the Moon
Hi Krishna,
You can call other class' methods from start method of your Batch class. Shouldn't be a problem.
public static Database.QueryLocator start(Database.BatchableContext context){
        
        SampleClass.debugMethod();
        System.debug('Start');
        return Database.getQueryLocator('select Id from Account limit 2');
        
    }
 
public class SampleClass {

    public static void debugMethod(){
        
        System.debug('Inside Sample');
        
    }

}
Let me know if this helps.
ANUTEJANUTEJ (Salesforce Developers) 
So Krishna, I have used the example in trailed with a simple hello world example and below is the code:
 
global class UpdateContactAddresses implements 
    Database.Batchable<sObject>, Database.Stateful {
    
    // instance member to retain state across transactions
    global Integer recordsProcessed = 0;
    global Database.QueryLocator start(Database.BatchableContext bc) {
        system.debug('Start');
        helloworld.printhello();
        return Database.getQueryLocator(
            'SELECT id,name from account'
        );
    }
    global void execute(Database.BatchableContext bc, List<Account> scope){
        // process each batch of records
        List<Contact> contacts = new List<Contact>();
        for (Account account : scope) {
            for (Contact contact : account.contacts) {
                contact.MailingStreet = account.BillingStreet;
                contact.MailingCity = account.BillingCity;
                contact.MailingState = account.BillingState;
                contact.MailingPostalCode = account.BillingPostalCode;
                // add contact to list to be updated
                contacts.add(contact);
                // increment the instance member counter
                recordsProcessed = recordsProcessed + 1;
            }
        }
        update contacts;
        System.debug('Execute');
        helloworld.printhello();
    }    
    global void finish(Database.BatchableContext bc){
        System.debug(recordsProcessed + ' records processed. Shazam!');
        AsyncApexJob job = [SELECT Id, Status, NumberOfErrors, 
            JobItemsProcessed,
            TotalJobItems, CreatedBy.Email
            FROM AsyncApexJob
            WHERE Id = :bc.getJobId()];
        System.debug(job+''+recordsProcessed);
        // call some utility to send email
        //EmailUtils.sendMessage(job, recordsProcessed);
    }    
}

First the Start part is executed and below is the screenshot.
User-added image

Then the execute part is runner below is the screenshot:
User-added image

The method I used is below:
public class helloworld {
    public static void printhello(){
        system.debug('hello World');
    }
}

I hope this helps. In case if this comes handy can you please choose this as best answer so that it can be used by others in the future.