Hey guys, today in this post we are going to learn about How to Write Schedule class to update custom field and Send Email if condition is met on custom object Using Apex Method in Salesforce.
Use the Apex scheduler and the Schedulable interface if you have specific Apex classes that you want to run on a regular basis, or to run a batch Apex job using the Salesforce user interface. To know more details about Schedule Apex Jobs, Click Here.
Final Output →
You can download file directly from github by Click Here.
Other related post that would you like to learn in Salesforce
Create Schedule Apex Class Controller
Step 1:- Create Apex Class : Schedule_customCredential.apxc
From Developer Console >> File >> New >> Apex Class
Schedule_customCredential.apxc [Apex Class Controller]
|
global class Schedule_customCredential Implements Schedulable{
public static void EXECUTE(SchedulableContext SC){
updateCustomCre();
}
Public Static List<String> emailListStr = NEW List<String>();
public static void updateCustomCre(){
try{
List<Custom_Credential__c> custCrdtItem = NEW List<Custom_Credential__c>();
List<Custom_Credential__c> custCrdtList = NEW List<Custom_Credential__c>();
custCrdtList = [SELECT Id, Name, Credential_Landers__c, ClientId__c, User__c, User__r.Email, User__r.Name, HDFC_Credential__c, HDFC_Credential__r.Name FROM Custom_Credential__c WHERE Credential_Landers__c =NULL];
FOR(Custom_Credential__c crt:custCrdtList){
Custom_Credential__c crtObj = NEW Custom_Credential__c();
crtObj.Credential_Landers__c='Custom Credential';
crtObj.Id=crt.Id;
custCrdtItem.add(crtObj);
emailListStr.add(crt.User__r.Email);
}
UPDATE custCrdtItem;
//system.debug('custCrdtItem## ' + custCrdtItem);
//system.debug('emailListStr## ' + emailListStr);
sendEmailtoUser(emailListStr);
}
catch(Exception e){
system.debug('Exception' + e.getMessage());
}
}
public static void sendEmailtoUser(List<String> emailStr){
try{
FOR(String eml:emailListStr){
Messaging.reserveSingleEmailCapacity(2);
Messaging.SingleEmailMessage mail = NEW Messaging.SingleEmailMessage();
String[] toAddresses = NEW String[] {eml};
mail.setToAddresses(toAddresses);
//String[] ccAddresses = NEW String[] {'test@test.com'};
//mail.setCcAddresses(ccAddresses);
//mail.setReplyTo('');
//mail.setSenderDisplayName('');
mail.setSubject('Custom Credential Update');
mail.setBccSender(FALSE);
mail.setPlainTextBody('Custom Credential Updated Successfully');
//mail.setHtmlBody('Your Link <a href=https://www.salesforce.com/>click here.</a>');
Messaging.sendEmail(NEW Messaging.SingleEmailMessage[] { mail });
}
}catch(Exception e){
system.debug('Exception '+ e.getMessage());
}
}
}
Further post that would you like to learn in Salesforce
How do I see scheduled Apex jobs in Salesforce?
To view this page, from Setup, enter Scheduled Jobs in the Quick Find box, then select Scheduled Jobs.
How many ways we can schedule the Apex?
To schedule an Apex job other than Queueable, you first need to implement the Schedulable interface for the particular Apex class. Then, you have two options: one, create the schedule using the Schedule Apex page in the user interface or two, schedule the Apex job through the developer console using system.
Can we schedule normal Apex class?
No you can not schedule the Normal Apex class. If you want to schedule any class you need to implements Schedulable interface.
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 |