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
❤Code❤Code 

multiple email from apex trigger received

I have written a trigger to send email. Whenever the trigger is fired i am getting equal no of mails which is equal to no of records retrieved.

Ex - If i am getting 3 records, three times i am receiving email. Can anybody tell me where i am going wrong . I want a single email to be fired.

trigger taskEmail on Project__c (after update ) {
    set<String> projName = new set<String>();
    list<Task__c> listTask = new list<Task__c>();
    
    for(Project__c pro : Trigger.new) {
        projName.add(pro.Name);
    }
    
    listTask = [SELECT Id,Project_Approval__c,Project_Name__c,Status__c,Task_Executioner__c FROM Task__c WHERE Project_Name__c IN : projName];
    system.debug('11111' + listTask.size());
    EmailTemplate et=[Select id from EmailTemplate where name='Multiple Tasks'];
    List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
    
    Set<Task__c> setTaskName = new Set<Task__c>();
    setTaskName.addAll(listTask);
    List<Task__c> finalTsk = new List<Task__c>();
    finalTsk.addAll(setTaskName);
    
    
    if(listTask.size() > 0) {
    system.debug('222222' + listTask.size());
        for(Task__c con : finalTsk) {
        system.debug('33333  enter for ');       
        if(con.Project_Approval__c =='Approved' && con.Status__c == 'Open'){
            system.debug('444   enter if inside for '); 
            //String userEmail = t.Task_Executioner__r.Email;
            //String[] toAddresses = new String[] {userEmail};    
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            //mail.setToAddresses(toAddresses);
            //mail.setSenderDisplayName('Test');
            mail.setTargetObjectId(con.Task_Executioner__c);
            //mail.setTargetObjectId(t.Task_Executioner_1__c);
            //mail.setTargetObjectId(t.Task_Executioner_2__c);
            mail.setTemplateId(et.Id);
            mail.setSaveAsActivity(false);
            mail.setWhatId(con.Id);
            mails.add(mail);
            //Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        }
        system.debug('0000000');
       
    }
    
    Messaging.sendEmail(mails);
}
}

Regards
Best Answer chosen by ❤Code
Thiyagarajan Selvaraj (SFDC Developer)Thiyagarajan Selvaraj (SFDC Developer)
Sorry, it's my mistake. Please try this now..
Set<Id> executionerIds = new Set<Id>();
for(Task__c con : listTask ) {
    system.debug('33333  enter for ');       
    if(con.Project_Approval__c =='Approved' && con.Status__c == 'Open' && !executionerIds.contains(con.Task_Executioner__c)){
        system.debug('444   enter if inside for '); 
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setTargetObjectId(con.Task_Executioner__c);
        mail.setTemplateId(et.Id);
        mail.setSaveAsActivity(false);
        mail.setWhatId(con.Id);
        mails.add(mail);
        executionerIds.add(con.Task_Executioner__c);
    }
    system.debug('0000000');

}

All Answers

Thiyagarajan Selvaraj (SFDC Developer)Thiyagarajan Selvaraj (SFDC Developer)
Is that the task Executioner receiving the same email multiple times?
Mohit Bansal6Mohit Bansal6
Just check the email template, did you assigned yourself at the recipient user in the email template.?

Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help
❤Code❤Code
Hi Thiyagarajan,

Yes the  task Executioner is receiving the email multiple times. Below is the updated code - The value present i listTask listg variable , that much email is triggered.

I need a single email to be trigered instead of multiple..


trigger taskEmail on Project__c (after update ) {
    set<String> projName = new set<String>();
    list<Task__c> listTask = new list<Task__c>();

    for(Project__c pro : Trigger.new) {
        projName.add(pro.Name);
    }

    listTask = [SELECT Id,Project_Approval__c,Project_Name__c,Status__c,Task_Executioner__c FROM Task__c WHERE Project_Name__c IN : projName];
    system.debug('11111' + listTask.size());
    EmailTemplate et=[Select id from EmailTemplate where name='Multiple Tasks'];
    List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();

    if(listTask.size() > 0) {
    system.debug('222222' + listTask.size());
        for(Task__c con : listTask ) {
        system.debug('33333  enter for ');       
        if(con.Project_Approval__c =='Approved' && con.Status__c == 'Open'){
            system.debug('444   enter if inside for '); 
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setTargetObjectId(con.Task_Executioner__c);
            mail.setTemplateId(et.Id);
            mail.setSaveAsActivity(false);
            mail.setWhatId(con.Id);
            mails.add(mail);
        }
        system.debug('0000000');

    }

    Messaging.sendEmail(mails);
}


Regards
Thiyagarajan Selvaraj (SFDC Developer)Thiyagarajan Selvaraj (SFDC Developer)
Hi sfdc.buddy,

I made a few changes in the for loop, try this 
Set<Id> executionerIds = new Set<Id>();
for(Task__c con : listTask ) {
    system.debug('33333  enter for ');       
    if(con.Project_Approval__c =='Approved' && con.Status__c == 'Open' && !executionerIds.contains(con.Task_Executioner__c)){
        system.debug('444   enter if inside for '); 
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setTargetObjectId(con.Task_Executioner__c);
        mail.setTemplateId(et.Id);
        mail.setSaveAsActivity(false);
        mail.setWhatId(con.Id);
        mails.add(mail);
        executionerIds.add();
    }
    system.debug('0000000');

}

 
❤Code❤Code
Hi ,

I m getting incorrect method error in the line no 12. can u plese check.

executionerIds.add();

Regards
❤Code❤Code
Hi,
I m still geting 3 emails.

Regards,
Thiyagarajan Selvaraj (SFDC Developer)Thiyagarajan Selvaraj (SFDC Developer)
Sorry, it's my mistake. Please try this now..
Set<Id> executionerIds = new Set<Id>();
for(Task__c con : listTask ) {
    system.debug('33333  enter for ');       
    if(con.Project_Approval__c =='Approved' && con.Status__c == 'Open' && !executionerIds.contains(con.Task_Executioner__c)){
        system.debug('444   enter if inside for '); 
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setTargetObjectId(con.Task_Executioner__c);
        mail.setTemplateId(et.Id);
        mail.setSaveAsActivity(false);
        mail.setWhatId(con.Id);
        mails.add(mail);
        executionerIds.add(con.Task_Executioner__c);
    }
    system.debug('0000000');

}
This was selected as the best answer
❤Code❤Code
Hi Thiyagarajan, 

Superb bro.U r genius. Worked like a charm. Thanks very much..

Regards..