You need to sign in to do that
Don't have an account?
Laaral
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
see Below sample code..
Batch Apex execute code:
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
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:
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/
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
can you share your batch apex code so i can tell you what is the mistake in this?
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 });
}
}
see Below sample code..
Batch Apex execute code:
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/
Thanks, now it works. So I should have use the batch class name for the BatchExec-parts of the code.