0

I want to make an email reminder if case status != closed to case owner every H+5, H+7, and H+10 ..

H = created date

and I want 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. .. how do i write the loop? this is what i`ve tried so far..

global class SchReminderActivitytoOwner implements Schedulable  {

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_Testing'];
    OrgWideEmailAddress owe = [SELECT ID,IsAllowAllProfiles,DisplayName,Address FROM OrgWideEmailAddress WHERE IsAllowAllProfiles = TRUE LIMIT 1];

    Date MyDate = Date.Today();
    String subject = 'Testing Reminder';                            
    String 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> listcase = [SELECT Id, ContactEmail, Owner_Name__c, Owner.Email, Status, Ticket_Number__c, CaseNumber, Subject, SLA_Case_Closed_Days__c, CreatedBy.Name, OwnerId
                           //FROM Case WHERE Email_Target__c = '[email protected]' and Status != 'Closed' Order by Owner.Name LIMIT 50000];

    List<User> us = [SELECT Id, Name, ProfileId, Profile.Name, isActive,Email FROM User 
                     WHERE Email = '[email protected]' and IsActive = TRUE]; //this is my email for testing purpose 


    Integer i = 1;
    Id OwnerLast;
    List<Case> listcase = new List<Case>();
    if(Test.isRunningTest()){
            listcase = [SELECT Id, ContactEmail, Owner_Name__c, Owner.Email, Status, Ticket_Number__c, CaseNumber, Subject, SLA_Case_Closed_Days__c, CreatedBy.Name,CreatedDate, OwnerId
                        FROM Case WHERE Email_Target__c = '[email protected]' LIMIT 1];
        }else{
            listcase = [SELECT Id, ContactEmail, Owner_Name__c, Owner.Email, Status, Ticket_Number__c, CaseNumber, Subject, SLA_Case_Closed_Days__c, CreatedBy.Name,CreatedDate, OwnerId
                        FROM Case WHERE Email_Target__c = '[email protected]' and Status != 'Closed' Order by Owner.Name LIMIT 50000];
        }

    if (listcase.size() > 0){
                for (Case cs : listcase){  

                    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__c);
                        /*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>';

                    //i++;

                    } 

                    //List<User> us = [SELECT Id, Name, ProfileId, Profile.Name, isActive,Email FROM User 
                                     //WHERE Email = '[email protected]' and IsActive = TRUE]; 

                    for (User us2 : us){ 
                        OwnerLast = us2.Id;


                        htmlBody += '</table><br><br><br><p>Terimakasih,</p><br><p>System Administrator</p>';

                        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 

                        //Id [] toaddress= New Id[]{};
                        mail.setOrgWideEmailAddressId(owe.Id);
                        mail.setSenderDisplayName('Administrator');
                        mail.setSubject(subject);
                        mail.setHtmlBody(htmlBody);
                        mail.setTargetObjectId(OwnerLast);
                        //mail.setToAddresses(toaddress);
                        mail.setSaveAsActivity(false);
                        listmail.add(mail);
                        Messaging.sendEmail(listmail);

                        }

            }

        }
}

and this is the result..

enter image description here

I manually select my own user based on my personal email value ([email protected]).. how do I select every owner of every cases? i tried using OwnerId but nothing happen..

3
  • The question is not clear. Please re-phrase. Send mail to whom? If a case is not closed, send a mail to the case owner?
    – S..
    Jul 5, 2018 at 7:31
  • my bad @N.B. .. I want to make a mail reminder if case status != closed to case owner every H+5, H+7, and H+10 .. H = created date Jul 5, 2018 at 7:35
  • 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 Jul 5, 2018 at 7:35

1 Answer 1

0

Please use the below code for sending email to the owner including all the cases he owns. We need to make use of map> for this to form the table. Hope this resolves the problem. Let me know if it works `global class SchReminderActivitytoOwner implements Schedulable { global void execute(SchedulableContext ctx) {

    List<Messaging.SingleEmailMessage> listmail = new List<Messaging.SingleEmailMessage>();
    EmailTemplate et = [SELECT Id, Subject, HtmlValue FROM EmailTemplate limit 1];

    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 FROM Case where Status != 'Closed']){

        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>' + PIC + '</td><td><a href="https://cs72.salesforce.com/' + Link + '">Click Here</a></td></tr>';

            mapOwnerEmailtoEmail.put(strOwnerEmail, htmlBody);
        }
    }

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

        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.setToAddresses(sendTo);
        mail.setSenderDisplayName('Administrator');
        mail.setSubject(subject);
        mail.setHtmlBody(strHTML);

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


    }
    Messaging.sendEmail(listmail);

}

}`

Let me know if it works.

11
  • Thanks for the answer.. i just run the code and got an error : Jul 5, 2018 at 8:36
  • Line: 74, Column: 1 System.EmailException: SendEmail failed. First exception on row 4; first error: INVALID_EMAIL_ADDRESS, Email address is invalid: null: [toAddresses, null] Jul 5, 2018 at 8:36
  • Check in debug what is coming into '=strOwnerEmail=='. Might be the email Id given is not correct. Please verify
    – Rashmi S
    Jul 5, 2018 at 9:02
  • there`s 1 null email value.. is this the problem? Jul 5, 2018 at 9:06
  • 16:04:27:418 USER_DEBUG [57]|DEBUG|===strOwnerEmail====null Jul 5, 2018 at 9:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .