Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to Write Schedule Class to update custom field and Send Email to User if condition is met on custom object in Salesforce #26

Open
vijayk3327 opened this issue Aug 27, 2023 · 0 comments
Assignees
Labels
documentation Improvements or additions to documentation question Further information is requested

Comments

@vijayk3327
Copy link
Owner

vijayk3327 commented Aug 27, 2023

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 Apex Triggers, Click Here →

👉 To get source code live demo link, Click Here.

Create Schedule Apex Class Controller
Step 1:- Create Apex Class : Schedule_customCredential.apxc

`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());
    }
}

}`

👉 To get source code live demo link, Click Here.

@vijayk3327 vijayk3327 added documentation Improvements or additions to documentation question Further information is requested labels Aug 27, 2023
@vijayk3327 vijayk3327 self-assigned this Aug 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation question Further information is requested
Projects
Status: No status
Development

No branches or pull requests

1 participant