I'm really confused by order of execution. I have tried with a few posts, but mine does not work. Please help with taking a look.
I am able to receive the templated email, but there's no incident fields
contents rendered in the email. The received email shows the No incidents to report
. If I set Test with merge fields, and provide static environment Id and contactId, then it works and show the binded incidents in the received email. The environmentId seems not passed into the template/controller.
The ultimate goal is to trigger email firing by scheduled apex job. In testing phase, I'm using following code to run, and can receive fired email. But the email content is No incidents to report
.
SenderForIncidents sender = new SenderForIncidents();
sender.execute(null);
public with sharing class SenderForIncidents Implements Schedulable{
public void execute(SchedulableContext sc){
// Queries prepared template, orgWideEmail, and contactList
EmailTemplate template = [SELECT Id FROM EmailTemplate WHERE... LIMIT 1];
OrgWideEmailAddress orgWideEmail = [SELECT Id FROM OrgWideEmailAddress WHERE ... LIMIT 1];
List<Contact> contactList = [SELECT ......];
List<Messaging.SingleEmailMessage> emailSendList = new List<Messaging.SingleEmailMessage>();
for (Contact c : contactList){
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setOrgWideEmailAddressId(orgWideEmail.Id);
email.setTemplateId(template.Id);
email.setTargetObjectId(c.Id);
email.saveAsActivity = false;
email.setWhatId(relatedToTypeObjectId); UPDATE-------> SOLUTION!!!!!!
emailSendList.add(email);
}
Messaging.sendEmail(emailSendList, false);
}
}
============ Below are email template, VF component, and controller info.===============
Email template
<messaging:emailTemplate subject="{!relatedTo.Name}"
recipientType="Contact"
relatedToType="Environment__c">
<c:MyController environmentId="{!relatedTo.Id}" />
</messaging:emailTemplate>
Visual force component
<apex:component controller="MyController" access="global">
<apex:attribute name="environmentId" type="Id"
description="Environment Id" assignTo="{!envId}" />
<apex:outputPanel rendered="{!!incidents.empty}">
<apex:repeat value="{!incidents}" var="incident">
<p>{!incident.fieldA__c}</p>
</apex:outputPanel>
<apex:outputPanel rendered="{!incidents.empty}">
<p> No Incidents to Report. </p>
</apex:outputPanel>
</apex:component>
controller
Suggested controller version from first answer.
public class MyController() {
public Id envId;
public void setEnvId(Id envIdValue) {
List<Object2> matchedObj2 = [SELECT Case__c FROM Object2 where Environment__c = :envId];
List<Id> caseIds = new List<Id>();
for (Object2 obj : matchedObj2) {
caseIds.add(obj.Case__c);
}
incidents = [SELECT ... FROM Case WHERE Id in :caseIds ];
}
public MyController(){}
public List<Case> incidents {get; set;}
}
My original version
/*public class MyController {
public Id envId {get; set;}
public MyController(){}
public List<Case> incidents;
public List<Case> getIncidents() {
if (incidents == null) {
List<Object2> matchedObj2 = [SELECT Case__c FROM Object2 where Environment__c = :envId];
List<Id> caseIds = new List<Id>();
for (Object2 obj : matchedObj2) {
caseIds.add(obj.Case__c);
}
incidents = [SELECT .... FROM Case WHERE Id in :caseIds AND ...];
}
return incidents;
}
}*/
I have tried to test with merge field in email template with EnvironmentId and contact, it works. Seems either the template is not associating to environment__c, or the value was not passed to the controller. Generous help appreciated. Thank you.
custom component
using editSend test and verify merge fields
with filled static envId and contact, there are incidents contents sent out. How can we testify that Environment__c is binded appropriately please? Or do you spot anything else I did not correct? Appreciate so much of your kindness and wisdom.