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+1Code+1 

Passing parameter in vf template

Hello All,

I have two custom objects Project__c and Task__c.
Task__c is having a lookup to Project__c object.
When New tasks are created and tasks are assigned to Task Executioner(Email Field)...  via workflow alert, we are sending mails to Task Executioner.
So this, if 10 tasks are created --- It triggers 10 email alerts.
Client needs only one consolidated mail to be sent.

Please let me know if there are any solution to this..
I have created a vf template and facing issues...

Thanks ! 
 
Nitin PaliwalNitin Paliwal
Hi,
Instead of using workflow to send the email alert.
You can use trigger to send the consolidated email to the Task Executioner.
You will have to maitain a map  like map<String,List<Task__c>> , here key would be the Task Executioner email address.
Iterate over this map and generate the mail body dynamically and send the email to the Task Executioner.

I hope this helps you.

Thanks
Nitin
 
Code+1Code+1
Hi Nitin,

 Please find the code which I have written to achieve this, so that it can give clarity for you.
 Issue is, when I send the Email Template using "Send Test and Verify" i am getting the desired output.
 But, when I try to send the Email Template from Trigger, i am not getting any values......

------------------------------------------------------
Apex Class
-----------------
public class send_NewTasks{   
  //capture the user id
    public ID salesRepID {get; set;}
    public List<Task__c> totalTasks ;

    public List<Task__c> gettotalTasks () {
        totalTasks= [select Name from Task__c where Status__c = 'Open' AND Project_Status_Approved__c = True AND Project__c = :salesRepID];
        system.debug('@@@@@@' + salesRepID );
        return totalTasks;
    }
}

------------------------------------------------------


VisualForce Component 
----------------------------------
<apex:component controller="send_NewTasks" access="global">
    <apex:attribute name="ToID" type="ID" description="New Tasks Assignment" assignTo="{!salesRepID}"/>
     <apex:outputText value="{!salesRepID}"></apex:outputText>
     {!totalTasks }
        <apex:datatable value="{!totalTasks }" var="item" border="1">
                    <apex:column headervalue=" Name ">
                        <apex:outputText value="{!item.name}"/>
                    </apex:column>
                 
       </apex:datatable>
</apex:component>
------------------------------------------------------


Visualforce Email Template
-----------------------------------
<messaging:emailTemplate subject="New Tasks Assignment" recipientType="User" relatedToType="Task__c">
<messaging:htmlEmailBody >
<c:send_NewTasks ToID="{!RelatedTo.Project__c}" />
</messaging:htmlEmailBody>
</messaging:emailTemplate>

------------------------------------------------------
Trigger code
----------------
trigger EmailService on Task__c (before update) {

EmailTemplate et=[Select id from EmailTemplate where name='Multiple Tasks'];

List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();

    for(Task__c t : Trigger.new)
    {
       
          If(t.Project_Status_Approved__c &&t.Status__c == 'Open' ){
          String userEmail = t.Task_Executioner1__c;
          String[] toAddresses = new String[] {userEmail};    
          Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
          system.debug('11111');
          mail.setToAddresses(toAddresses);
          mail.setSenderDisplayName('Test');
          mail.setTargetObjectId('0031100000dtg5C');
          mail.setTemplateId('00X11000000IZhv');
          mail.setSaveAsActivity(true);
          mails.add(mail);
           
       }
    }
    system.debug('2222222');
 Messaging.sendEmail(mails);
}