function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Arief GunawanArief Gunawan 

Help scheduled email reminder on case

Hi, Im new in salesforce programming and need some help, or reference..
I want to make a mail reminder that automatically send the email to my sales user every h+7 h+10 and h+15 created date.. how do I code the scheduler? using IF?

for example, I want this mail reminder on case object send automatically to case owner every h+7 h+10 and h+15 created date..
global class SchReminderActivitytoOwner implements Schedulable  {


public static String CRON_EXP = '0 0 0 3 9 ? 2022';
global void execute(SchedulableContext ctx) {

List<Messaging.SingleEmailMessage> listmail = new List<Messaging.SingleEmailMessage>();
EmailTemplate et = [SELECT Id, Subject, HtmlValue FROM EmailTemplate WHERE developerName = 'ET_Case_Reminder_Test22'];

String subject = 'Testing Reminder';                            
String htmlBody ='';


map<string, list<case>> mapOwnerEmailtoCaseList = new map<string, list<case>>();
map<string, user> mapEmailtoUser = new map<string, user>();
for(Case objCase : [SELECT Id, Owner.Email, Status, Owner.Name, CaseNumber, CreatedBy.Name,Subject, OwnerId, SLA_Case_Closed_Days__c  FROM Case where Status != 'Closed' AND Owner.Type = 'User']){

    if(mapOwnerEmailtoCaseList.containsKey(objCase.Owner.Email)) {
        List<Case> lstCase = mapOwnerEmailtoCaseList.get(objCase.Owner.Email);
        lstCase.add(objCase);
        mapOwnerEmailtoCaseList.put(objCase.Owner.Email, lstCase);
    } else {
        mapOwnerEmailtoCaseList.put(objCase.Owner.Email, new List<Case> { objCase });
    }
}               

for(User objUser : [SELECT Id, Name, ProfileId, Profile.Name, isActive,Email FROM User 
                    WHERE Email =: mapOwnerEmailtoCaseList.keyset() and IsActive = TRUE]){

    mapEmailtoUser.put(objUser.Email, objUser);
}        

map<string, string> mapOwnerEmailtoEmail = new map<string, string>();        
for(string strOwnerEmail : mapOwnerEmailtoCaseList.keyset()){

    User objUser = mapEmailtoUser.get(strOwnerEmail);
    htmlBody = '<h1>***JANGAN DIBALAS/DO NOT REPLY***</h1><br>Dear {!Case.OwnerFullName}, <br>Mohon untuk menindaklanjuti pengajuan interaksi berikut ini sebelum melewati waktu SLA :<br><br><table border="1" style="border-collapse: collapse"><tr><th><b>Ticket Number</b></th><th><b>Subject</b></th><th><b>SLA Handling Time Target</b></th><th><b>Created By</b></th><th><b>Detail</b></th></tr>';
    list<Case> lstCase = mapOwnerEmailtoCaseList.get(strOwnerEmail);
    for(Case cs : lstCase){

        String TicketNumber = String.Valueof(cs.CaseNumber); if(cs.CaseNumber == null){TicketNumber = '[Not Provided]';}
        String Sbj = cs.Subject; if(cs.Subject == null){Sbj = '[Not Provided]';}
        String HandlingTimeTarget = String.Valueof(cs.SLA_Case_Closed_Days__c); if(cs.SLA_Case_Closed_Days__c == null){TicketNumber = '[Not Provided]';}
        String Link = cs.Id; if(cs.Id == null){Link = '[Not Provided]';}
        String PIC = cs.CreatedBy.Name; if(cs.CreatedBy.Name == null){PIC = '[Not Provided]';}

        htmlBody = htmlBody.replace('{!Case.OwnerFullName}', cs.Owner.Name);
        /*htmlBody = htmlBody.replace('{!Case.Ticket_Number__c}', String.Valueof(cs.CaseNumber));
        //htmlBody = htmlBody.replace('{!Case.Subject}', cs.Subject);
        //htmlBody = htmlBody.replace('{!Case.SLA_Handling_Time_Target__c}', String.Valueof(cs.SLA_Case_Closed_Days__c)); */        
        htmlBody += '<tr><td>' + TicketNumber + '</td><td>' + Sbj + '</td><td>' + HandlingTimeTarget + '</td><td>' + PIC + '</td><td><a href="https://cs72.salesforce.com/' + Link + '">Click Here</a></td></tr>';

            mapOwnerEmailtoEmail.put(strOwnerEmail, htmlBody);

        mapOwnerEmailtoEmail.put(strOwnerEmail, htmlBody);

    }
}

for(string strOwnerEmail : mapOwnerEmailtoCaseList.keyset()){

    if(strOwnerEmail != null){
    system.debug('===strOwnerEmail===='+strOwnerEmail);
    string strHTML = mapOwnerEmailtoEmail.get(strOwnerEmail);
    strHTML += '</table><br><br><br><p>Terimakasih,</p><br><p>System Administrator</p>';
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 

    List<String> sendTo = new List<String>();
    sendTo.add(strOwnerEmail);
    //mail.setTargetObjectId(sendTo);
    mail.setToAddresses(sendTo);
    mail.setSenderDisplayName('Administrator');
    mail.setSubject(subject);
    mail.setHtmlBody(strHTML);

    mail.setSaveAsActivity(false);
    listmail.add(mail);

    }
    }
    Messaging.sendEmail(listmail);
}
 }

 
Arief GunawanArief Gunawan
the purpose of that example code is to make a table (as email body), so the email body = list of cases that the owner has (sending 1 email with the list of cases to 1 owner), instead of sending 1 email for 1 case to 1 owner.. it works fine, just need to add some scheduler..
Raj VakatiRaj Vakati
Schedule as below 
h+10 ;

SchReminderActivitytoOwner testobj = new SchReminderActivitytoOwner ();
String cronexpression = ‘0 0 10 * * * *’
System.schedule(‘Testing’, cronexpression, testobj);

h+15

SchReminderActivitytoOwner testobj = new SchReminderActivitytoOwner ();
String cronexpression = ‘0 0 15 * * * *’
System.schedule(‘Testing’, cronexpression, testobj);


h+23

SchReminderActivitytoOwner testobj = new SchReminderActivitytoOwner ();
String cronexpression = ‘0 0 23 * * * *’
System.schedule(‘Testing’, cronexpression, testobj);