[SalesForce] Apex utility class for email send

I have below method to send email. Now I need to do the same from a separate utility class where I will have to pass toAddress , ccaddress , parameter string which has a map with Email template , suppose (A -> 'Email Template 1' , B -> 'Email Template 2' etc.)

Can someone let me know how to create such an utility or if such available to feed directly?

Publish method:

public void Publish() {
    Set < String > tempSet = new Set < String > ();
    List < String > sendTo = new List < String > ();
    List < ID > CIdlist = new List < ID > (); //Send to future
    chkpublish = true;
    ispublish = true; //Connfirm button hide
    List < Compliance_Incident_abv__c > selIncs = (List < Compliance_Incident_abv__c > ) contr.getSelected();
    for (Compliance_Incident_abv__c inc: selIncs) {

        inc.status_abv__c = 'Open';
        inc.Publish_Date_abv__c = Date.today();
        if (inc.Rep_abv__c != null) inc.OwnerId = inc.Rep_abv__c;

        CIdlist.add(inc.id); //Add id
    }
    update selIncs;

    List < Compliance_Incident_Transaction_abv__c > CIRec = [select ID, Name, Call_abv__c from Compliance_Incident_Transaction_abv__c where Compliance_Incident_abv__c in : selIncs];
    for (Compliance_Incident_Transaction_abv__c CT: CIRec) {
        if (CT.Call_abv__c != null) CT.Publish_Check_abv__c = true;
    }

    update CIRec;
    /*Map<Id,String> ManagerMap= new Map<Id,String>(); 
    Map<Id,String> RepMap= new Map<Id,String>(); 
    Map<Id,String> RepNameMap= new Map<Id,String>(); */
    Map < Id, Compliance_Incident_abv__c > OneMap = new Map < Id, Compliance_Incident_abv__c > ();
    for (Compliance_Incident_abv__c myContact: [select ID, ManagerEmail_abv__c, RepEmail_abv__c, Rep_abv__c, Rep_abv__r.Name, Rep_abv__r.ManagerId, Rep_abv__r.Manager.Name, Name from Compliance_Incident_abv__c where ID in : selIncs]) {

        OneMap.put(mycontact.id, mycontact);
        system.debug('OneMap@' + OneMap);

    }

    List < Messaging.SingleEmailMessage > mails = new List < Messaging.SingleEmailMessage > ();
    List < Messaging.SingleEmailMessage > mailsDM = new List < Messaging.SingleEmailMessage > ();
    for (Compliance_Incident_abv__c myContact: selIncs) {
        sendTo.clear();
        ccTo.clear();
        sendToDM.clear();

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

        Messaging.SingleEmailMessage mailDM = new Messaging.SingleEmailMessage();
        sendTo.add(OneMap.get(mycontact.ID).RepEmail_abv__c);
        /*  SendToSet.addall(sendTo);
          SendTo.removeAll();
          SendTo.AddAll(SendToSet);
          */

        mail.setToAddresses(sendTo);
        sendToDM.add(OneMap.get(mycontact.ID).ManagerEmail_abv__c);
        mailDM.setToAddresses(sendToDM);
        mail.setSenderDisplayName('Abbvie IT Team');
        mail.SetSaveAsActivity(false);
        mailDM.setSenderDisplayName('Abbvie IT Team');
        mailDM.SetSaveAsActivity(false);
        ccTo.add(OneMap.get(mycontact.ID).ManagerEmail_abv__c);
        if (OneMap.get(mycontact.ID).ManagerEmail_abv__c != null) {
            mail.setCcAddresses(ccTo);
        }
        Manage = OneMap.get(mycontact.ID).ManagerEmail_abv__c;
        //  mail.setTargetObjectId(OneMap.get(mycontact.ID).Rep_abv__c);
        // mailDM.setTargetObjectId(OneMap.get(mycontact.ID).Rep_abv__r.ManagerID);
        mail.setSubject('Attention Required:  NEW ABS incident ' + OneMap.get(mycontact.ID).Name + ' in iREP'); 
        mailDM.setSubject('Action Required: NEW ABS incident ' + OneMap.get(mycontact.ID).Name + ' in iREP'); 
        String body = 'Dear ' + OneMap.get(mycontact.ID).Rep_abv__r.Name + ', ';
        body += '<br><br>What you need to know: ';
        body += '<br>New ABS Incident(s) have been generated in iREP. Please review these incident(s) with your DM or Applicable Manager. There is no other action for you to complete in iREP.';
        body += '<br>To view your open incident(s):<br>';
        body += '<ul style="list-style-type:disc"><li>Log into iREP Online and select "Compliance Incidents."</li><li>Select "Open Incidents" in the drop down.</li><li>Click the Incident ID to view details of your incident(s).</li></ul>If you need assistance, please call the Field Help Desk at 1-800-344-6776.<br>';

        String bodyDM = 'Dear ' + OneMap.get(mycontact.ID).Rep_abv__r.Manager.Name + ', ';
        bodyDM += '<br><br>What you need to know:';
        bodyDM += '<br>New ABS Incident(s) have been published in iREP for a direct report.';
        bodyDM += '<br>What you need to do:'; //Bullets
        bodyDM += '<ul style="list-style-type:disc"><li>Review the incident(s) in iREP by clicking on "Compliance Incidents" and selecting "Open Incidents" in the View drop down.</li><li>Coach your direct report.</li><li>Attest to the incident and submit within two weeks, per ABS policy.</li></ul>';
        bodyDM += 'If you need assistance, please call the Field Help Desk at 1-800-344-6776.';
        //body1='<p style="font-family:Calibri"> body</p>';
        mail.setHtmlBody('<style>.myStyle{font-family:Calibri;font-size: 14.7px;}</style><span class="myStyle" >' + body + '</span>');
        // mail.setHtmlBody('<p style="font-family:Calibri"> </p>');
        mails.add(mail);
        mailDM.setHtmlBody('<style>.myStyle{font-family:Calibri;font-size: 14.7px;}</style><span class="myStyle" >' + bodyDM + '</span>');
        if (OneMap.get(mycontact.ID).ManagerEmail_abv__c != null) {
        mailsDM.add(mailDM);
        }
        system.debug('@mails@' + mails);

    }
    // Step 6: Send all emails in the master list*/
    Messaging.sendEmail(mails);
    //if (Manage != null) {
        Messaging.sendEmail(mailsDM); //Change incorporated
    //}
    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, 'Successfully Published'));

    //11th- Future Callout asynch.
    //Call_StamperFutureClass CSFuture= new Call_StamperFutureClass();
    //  Call_StamperFutureClass.CIFutureMethod(CIdlist);//Call future method with List of CI ids to be proceesed asynch.
    // CallBatch M = new CallBatch ();
    //Database.executeBatch(M);  
    Datetime sysTime = System.now();
    sysTime = sysTime.addSeconds(300); //add 5min in current time
    String chron_exp = '' + sysTime.second() + ' ' + sysTime.minute() + ' ' + sysTime.hour() + ' ' + sysTime.day() + ' ' + sysTime.month() + ' ? ' + sysTime.year();
    //Format

    CallStamperScheduler CallStamperScheduler1 = new CallStamperScheduler(); //CALL Scheduler after a delay of chron. set 600s
    String jobID = System.schedule('Compliance Incident Sch ' + sysTime.getTime(), chron_exp, CallStamperScheduler1);
    System.debug('==Scheduler Job ID==' + jobId);


}

Best Answer

You would create a class that looks something like this:

public with sharing class MyEmailHandler{

//declare public variables (possibly with {get;} but no setters)

    //constructor

    public void MyEmailHandler(list<string> toAddresses, list<string> ccaddresses, map<string,string>param2Emailtemplate){

        // You can use this.toAddresses = toAddresses type notation if you wish 
        // provided you declared the variables in 1st section above.

        // You can call a method to process the above variables, simply put all the code here
        // from your original class, or allow that code to follow below. 

        // It's unclear to me which portions you want to include, so I've omitted.


    { 

    //You could also use a no constructor class and just declare a method as below that 
    //you go directly to OR just do all the work in your constructor as above
    // except the DML which cannot be done in a constructor.

    public void MyEmailHandlerMethod(list<string> toAddresses, list<string> ccaddresses, map<string,string>param2Emailtemplate){

       // code for processing the above data that also does the email DML or calls other methods
       // to do various parts of the work, but returns nothing (thus the reason for keyword void). 

    }

}

Note: I don't think you need to use the static keyword, only void. I could be mistaken on not needing to use static.

Related Topic