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
LaaralLaaral 

How to run the batch apex class

I made a batch class which fetched and updates objects information, and now I'm wondering should I make a trigger to run the batch class or should I make a scheduler class ? Could you give me an example of a trigger that calls a batch and how to create a scheduler class ? I have been reading about them , but I'm not sure what to do and where to start.

 

Thanks!

Laaral

Best Answer chosen by Admin (Salesforce Developers) 
hitesh90hitesh90

see Below sample code..

 

Batch Apex execute code:

 String Query = 'select Id, Fault_Classification__c, Setup_Name__c,'+ 'CaseNumber,Related_Setup__c,Status_Explanation__c '+
    'from Case '+
    'where RecordTypeId ='+ recordType.Id+
    'and IsClosed = false'+
    'order by CreatedDate desc';
ImplementingServiceAvailabilityUpdater  objBatchServiceAvailability= new ImplementingServiceAvailabilityUpdater ();
Id batchInstanceId = Database.executeBatch(objBatchServiceAvailability, 10);

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator
My Blog:- http://mrjavascript.blogspot.in/

 

All Answers

hitesh90hitesh90

Hi Laaral,

 

You can execute your batch class as per your requirements.
1) it's required on sceduling (use schedule apex)
2) Only one time execution (use developer console)
3) On any DML operation (use trigger)


Below is the sample Example of Execution of batch apex.
you can execute it in developer console also..

 

Sample Code:

String Query = 'Select Mass_Email__r.Mail_To__c,Mass_Email__r.EmailTemplate__c,Mass_Email__r.Mail_From__c,Contact__c,Lead__c from Mass_Email_Detail__c where Mass_Email__c=\''+MassEmailID+'\'';
BatchExecMassEmail  objBatchEmail= new BatchExecMassEmail(Query);
Id batchInstanceId = Database.executeBatch(objBatchEmail, 10);

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator
My Blog:- http://mrjavascript.blogspot.in/

 

LaaralLaaral

So I would do it like this with developer console ?

 

String Query = 'select Id, Fault_Classification__c, Setup_Name__c,'+ 'CaseNumber,Related_Setup__c,Status_Explanation__c '+
    'from Case '+
    'where RecordTypeId ='+ recordType.Id+
    'and IsClosed = false'+
    'order by CreatedDate desc';
BatchExecServiceAvailability  objBatchServiceAvailability= new BatchExecServiceAvailability(Query);
Id batchInstanceId = Database.executeBatch(objBatchServiceAvailability, 10);

 

and  it's saying to me that

line 6, column 42: Invalid type: BatchExecServiceAvailability. What type it needs it to be so it can run it?
hitesh90hitesh90

can you share your batch apex code so i can tell you what is the mistake in this?

LaaralLaaral

global class ImplementingServiceAvailabilityUpdater implements Database.Batchable<sObject>{

     global ImplementingServiceAvailabilityUpdater(){
        // Batch Constructor
        }
    
        // Start Method
        global Database.QueryLocator start(Database.BatchableContext BC){
        
        String query = 'select Id, Fault_Classification__c, Setup_Name__c,'+ 'CaseNumber,Related_Setup__c,Status_Explanation__c '+
        'from Case '+
        'where RecordTypeId ='+ recordType.Id+
        ' and IsClosed = false'+
        ' order by CreatedDate desc';
 
        return Database.getQueryLocator(query);
        
          }
        

       // Fetches case objects

       global void execute(Database.BatchableContext BC, List<sObject> scope){
             for(Sobject sObj : scope){
              Case caseObj = (Case)sObj;
        //Now you can do what logic you want to implement with the case records you fetched.

        }   

    }    
        global void finish(Database.BatchableContext BC){

              // Logic to be Executed at finish
         // Create and send an email with the results of the batch.
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
 
           String[] toAddresses = new String[] {'laaral@test.com'};
        mail.setSenderDisplayName('Salesforce Support');
          mail.setSubject('New Case Created : ' + case.Id);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
 
         }

      }

hitesh90hitesh90

see Below sample code..

 

Batch Apex execute code:

 String Query = 'select Id, Fault_Classification__c, Setup_Name__c,'+ 'CaseNumber,Related_Setup__c,Status_Explanation__c '+
    'from Case '+
    'where RecordTypeId ='+ recordType.Id+
    'and IsClosed = false'+
    'order by CreatedDate desc';
ImplementingServiceAvailabilityUpdater  objBatchServiceAvailability= new ImplementingServiceAvailabilityUpdater ();
Id batchInstanceId = Database.executeBatch(objBatchServiceAvailability, 10);

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator
My Blog:- http://mrjavascript.blogspot.in/

 

This was selected as the best answer
LaaralLaaral

Thanks, now it works. So I should have use the batch class name for the BatchExec-parts of the code.

 

 

 

 

 

Anudeep BAnudeep B
What is callout in batch apex.. what it works for?