0

Below is the piece of code to send an email to already approved approvers of the record. I am getting error while saving the class.

/*Apex class Name - SGChinaIntDocCREmailAlert */
/*Description - Class implemented for Sanofi China Project. Class implemented for sending email alert on document add/update.*/

Public class SGChinaIntDocCREmailAlert{

    @InvocableMethod public static void Interid(list<id> ids){
        List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
        list<ahm__interaction__c> interlist =  [Select id,ownerid,ahm__Interaction_Type__c,ahm__Start_Date__c,ahm_SG_ClientInteractionCode__c,ahm__Interaction_Code_Label__c,ahm__Description__c,ahm__Interaction_Type_r__c,(Select actor.email From ProcessSteps where stepstatus = 'Approved' ) From ahm__interaction__c WHERE id in: ids];
        List<EmailTemplate> lstEmailTemplates = [SELECT Id, Body, Subject from EmailTemplate where DeveloperName = 'One_Time_Event_Mandatory_Document_Added_Notification_to_approvers'];
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setTemplateId(lstEmailTemplates[0].Id);
        mail.setSaveAsActivity(false);
        //mail.setTargetObjectId(interlist.OwnerId);// Any contact or User id of your record
        set<string> toemail = new set<string>();
        for(processinstanceHistory ps : interlist[0].getsobjects('ProcessSteps')){    
        system.debug(ps.actor.email);
        toemail.add(ps.actor.email);
        }
        mail.setToAddresses(toemail);
        mail.setWhatId(interlist[0].id); // Enter your record Id whose merge field you want to add in template
        Messaging.SendEmailResult[] resultMail = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
}

Below are the errors i am getting while saving.

  1. Method does not exist or incorrect signature: void setToAddresses(Set) from the type Messaging.SingleEmailMessage
0

A Set, while it is conceptually a Collection, is not a List, and can't be used in places where you'd use a List. Fortunately, there is a List constructor that accepts a Set, so you can convert your Set to a List with a simple modification:

    mail.setToAddresses(new List<String>(toemail));

This will allow you to keep your list de-duplicated and still allow you to use this method.

You should avoid using setToAddresses when emailing users, because you will consume the daily email limit. Instead, create multiple emails, add them all to a List, and then send all the messages at once. Alternatively, use the MassEmailMessage object instead, which would allow you to specify hundreds of users at once using setTargetObjectIds.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.