How to Write Schedule Class to update custom field and Send Email to User if condition is met on custom object in Salesforce | Schedule jobs using the apex scheduler in Salesforce | How To Schedule Apex Class/Job and send Email notification in Salesforce

1,677 views

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 β†’

scheduled apex job in salesforce -- w3web.net

 

You can download file directly from github by Click Here.

 

Other related post that would you like to learn in Salesforce

scheduled apex job in salesforce -- w3web.net
 

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]

  1.   global class Schedule_customCredential Implements Schedulable{
  2.     public static void EXECUTE(SchedulableContext SC){
  3.         updateCustomCre();
  4.     }
  5.  
  6.     Public Static List<String> emailListStr = NEW List<String>();
  7.  
  8.     public static void updateCustomCre(){
  9.         try{  
  10.             List<Custom_Credential__c> custCrdtItem = NEW List<Custom_Credential__c>();  
  11.             List<Custom_Credential__c> custCrdtList = NEW List<Custom_Credential__c>();
  12.             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];
  13.             FOR(Custom_Credential__c crt:custCrdtList){
  14.                 Custom_Credential__c crtObj = NEW Custom_Credential__c();
  15.                 crtObj.Credential_Landers__c='Custom Credential';
  16.                 crtObj.Id=crt.Id;
  17.                 custCrdtItem.add(crtObj);
  18.                 emailListStr.add(crt.User__r.Email);            
  19.             }
  20.             UPDATE custCrdtItem;
  21.             //system.debug('custCrdtItem## ' + custCrdtItem);
  22.             //system.debug('emailListStr## ' + emailListStr);
  23.  
  24.             sendEmailtoUser(emailListStr);
  25.         }
  26.         catch(Exception e){
  27.             system.debug('Exception' + e.getMessage());
  28.         }
  29.     }
  30.  
  31.  
  32.     public static void sendEmailtoUser(List<String> emailStr){
  33.         try{
  34.             FOR(String eml:emailListStr){
  35.                 Messaging.reserveSingleEmailCapacity(2);       
  36.                 Messaging.SingleEmailMessage mail = NEW Messaging.SingleEmailMessage();
  37.                 String[] toAddresses = NEW String[] {eml};
  38.                     mail.setToAddresses(toAddresses);
  39.                 //String[] ccAddresses = NEW String[] {'test@test.com'};
  40.                 //mail.setCcAddresses(ccAddresses);
  41.                 //mail.setReplyTo('');
  42.                 //mail.setSenderDisplayName('');
  43.                 mail.setSubject('Custom Credential Update');
  44.                 mail.setBccSender(FALSE);
  45.                 mail.setPlainTextBody('Custom Credential Updated Successfully');
  46.                 //mail.setHtmlBody('Your Link <a href=https://www.salesforce.com/>click here.</a>');
  47.                 Messaging.sendEmail(NEW Messaging.SingleEmailMessage[] { mail }); 
  48.             }
  49.  
  50.         }catch(Exception e){
  51.             system.debug('Exception '+ e.getMessage());
  52.         }
  53.     }
  54.  
  55. }

Further post that would you like to learn in Salesforce

 

 

FAQ (Frequently Asked Questions)

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



Hi, This is Vijay Kumar behind the admin and founder of w3web.net. I am a senior software developer and working in MNC company from more than 8 years. I am great fan of technology, configuration, customization & development. Apart of this, I love to write about Blogging in spare time, Working on Mobile & Web application development, Salesforce lightning, Salesforce LWC and Salesforce Integration development in full time. [Read full bio] | | The Sitemap where you can find all published post on w3web.net

Leave a Comment