Hey guys, today in this post we are going to learn about how to write batch class to update records/fields and send email notification in finish method on custom Object in Salesforce
Real time scenarios:- Write a batch class on Custom Object(scoreCard__c) to update Record’s Field through Anonymous Window in Salesforce.
- Don’t forget to check out:- How to write a schedule for this batch Click Here For More Information
Files we used in this post example
batch_scoreCardEmailUpdate.apxc | Apex Class Controller | It is used for update Recordβs Field through Anonymous Window in Salesforce |
Live Demo
You can download file directly from github by Click Here.
Other related post that would you like to learn in Salesforce
Create Apex Class Controller
Step 1:- Create Apex Class : batch_scoreCardEmailUpdate
From Developer Console >> File >> New >> Apex Class
batch_scoreCardEmailUpdate [Apex Class Controller]
global class batch_scoreCardEmailUpdate Implements DATABASE.Batchable<sobject> {
global DATABASE.QueryLocator START(DATABASE.BatchableContext BC){
string query = 'Select Id, Name, Email__c, City__c, Phone_No__c From scoreCard__c Where Name !=null';
RETURN DATABASE.getQueryLocator(query);
}
global void EXECUTE(DATABASE.BatchableContext BC, List<scoreCard__c> scope){
List<scoreCard__c> scoreUpate = NEW List<scoreCard__c>();
FOR(scoreCard__c scoreObj: scope){
scoreObj.Email__c = 'w3webmart@gmail.com';
scoreObj.City__c = 'Delhi';
scoreUpate.add(scoreObj);
}
system.debug('scoreUpate@@ ' + scoreUpate);
UPDATE scoreUpate;
}
global void finish(DATABASE.BatchableContext BC){
AsyncApexJob a = [SELECT Id, STATUS, NumberOfErrors, JobItemsProcessed,
TotalJobItems, CreatedBy.Email
FROM AsyncApexJob WHERE Id =
:BC.getJobId()];
Messaging.SingleEmailMessage mail = NEW Messaging.SingleEmailMessage();
String[] toAddresses = NEW String[] {a.CreatedBy.Email};
mail.setToAddresses(toAddresses);
mail.setSubject('Apex Sharing Recalculation schedule email update' + a.Status);
mail.setPlainTextBody
('The batch Apex job processed scoreCard update ' + a.TotalJobItems +
' batches with '+ a.NumberOfErrors + ' failures.');
Messaging.sendEmail(NEW Messaging.SingleEmailMessage[] { mail });
}
}
Create a instance of batch class and execute Anonymous Window in Salesforce
Step 2:- Open Apex Class >> batch_scoreCardEmailUpdate >> Debug >> execute batch class in Anonymous Window in salesforce
From Developer Console >> Debug >> execute Anonymous Window
For Anonymous Window
batch_scoreCardEmailUpdate scoreCard_batch = NEW batch_scoreCardEmailUpdate();
DATABASE.executeBatch(scoreCard_batch,10);
Further post that would you like to learn in Salesforce
How many records we can process using batch apex?
A maximum of 50 million records can be returned in the QueryLocator object. If more than 50 million records are returned, the batch job is immediately terminated and marked as Failed.
How does batch Apex work?
Each time you invoke a batch class, the job is placed on the Apex job queue and is executed as a discrete transaction. This functionality has two awesome advantages: Every transaction starts with a new set of governor limits, making it easier to ensure that your code stays within the governor execution limits.
How do I test a batch Apex class?
When testing your batch Apex, you can test only one execution of the execute method. You can use the scope parameter of the executeBatch method to limit the number of records passed into the execute method to ensure that you aren't running into governor limits. The executeBatch method starts an asynchronous process.
Related Topics | You May Also Like
Our Free Courses β
π Get Free Course β
π Salesforce Administrators π Salesforce Lightning Flow Builder π Salesforce Record Trigger Flow Builder |
π Get Free Course β
π Aura Lightning Framework π Lightning Web Component (LWC) π Rest APIs Integration |