You need to sign in to do that
Don't have an account?

VF Email Template Called by Trigger - {!Relatedto.field__c} not working on first iteration
I have two triggers-the first populates an object based on an XML document; the second kicks off an approval process based upon some of the fields populated by the first. The approval process sends out a VF email template that references some of the fields populated by the first trigger in this fashion - {!Relatedto.field__c} For some reason, these field values are not appearing in the email sent out to the first approver-however; they DO appear in all emails sent to the next approver once the first has given his/her approval. Could these be because the trigger has not yet fully completed saving the fields when the first email is sent and does so while SF waits for the first-level approver's response? I'm quite confused about this issue so any help you can provide would be much appreciated-below are the two triggers and the VF email template (Note: Some of the code that isn't HTML friendly might be cut-off by the forum); thanks!
trigger fillApprovers on Loan_Application__c (before insert, before update) {
list<Loan_Application__c> theLoanApps = new list<Loan_Application__c>();
//Append all batched Loan Applications to a Loan App list (Bulkifying the trigger)
for (Loan_Application__c aLoanApp:trigger.new){
theLoanApps.add(aLoanApp);
}
//Populate the fields in each loan app contained within the list
for (Integer i=0; i<theLoanApps.size(); i++){
//Check that the fields are not already populated by ensuring name field is null
if (theLoanApps[i].Name__c==null){
//Utilize Dynamic Apex to dynamically create the query that returns the XML document to be parsed
list<Document> docResult = [SELECT Body FROM Document WHERE Name LIKE :'LoanApp'+theLoanApps[i].ID_Number__c];
//Convert the body (of type Blob) of the document resulting from the SOQL query to a string
String xml = docResult[0].Body.toString();
//Create a Dom.Document from the xml string
Dom.Document doc = new Dom.Document();
doc.load(xml);
//Define root element
Dom.XMLNode loanApp = doc.getRootElement();
//Populate list of approvers while simultaneously removing this information from the document
list<String> approvers = new list<String>();
while (loanApp.getChildElement('Approver',null) != null) {
approvers.add(loanApp.getChildElement('Approver',null).getText());
loanApp.removeChild(loanApp.getChildElement('Approver',null));
}
//Repeat the while loop logic above for the other fields using if statements
if (loanApp.getChildElement('OrderDate',null) != null) {
String orderDate = loanApp.getChildElement('OrderDate',null).getText();
theLoanApps[i].Date__c = orderDate;
}
if (loanApp.getChildElement('Name',null) != null) {
String name = loanApp.getChildElement('Name',null).getText();
theLoanApps[i].Name__c = name;
}
if (loanApp.getChildElement('Address',null) != null) {
String address = loanApp.getChildElement('Address',null).getText();
theLoanApps[i].Address__c = address;
}
if (loanApp.getChildElement('Score',null) != null) {
String score = loanApp.getChildElement('Score',null).getText();
theLoanApps[i].Credit_Score__c = score;
}
if (loanApp.getChildElement('History',null) != null) {
String history = loanApp.getChildElement('History',null).getText();
theLoanApps[i].Credit_History__c = history;
}
if (loanApp.getChildElement('Notes',null) != null) {
String notes = loanApp.getChildElement('Notes',null).getText();
theLoanApps[i].Notes__c = notes;
}
//Iterate through approvers list, updating the corresponding fields of the loan application
//Depending on the approvers list size, some of the loan app's custom approver fields may be left blank
if (approvers.size()>=1) {
theLoanApps[i].Approver1__c=[SELECT Id FROM User WHERE Name = :approvers[0]][0].Id;
if (approvers.size()>=2) {
theLoanApps[i].Approver2__c=[SELECT Id FROM User WHERE Name = :approvers[1]][0].Id;
if (approvers.size()>=3) {
theLoanApps[i].Approver3__c=[SELECT Id FROM User WHERE Name = :approvers[2]][0].Id;
if (approvers.size()==4) {
theLoanApps[i].Approver4__c=[SELECT Id FROM User WHERE Name = :approvers[3]][0].Id;
}
}
}
}
}
}
}
trigger Approval_Process on Loan_Application__c (after insert, after update) {
//Bulkify the trigger
for (Loan_Application__c loanapp : Trigger.new){
//Check that the loan app is not currently awaiting approval
if (loanapp.Approval_Status__c!='Submitted for Approval' && loanapp.Approval_Status__c!='Approved'){
//Override Scenario
if (loanapp.Name__c=='Johnny Good'){
Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
req.setObjectId(loanapp.Id);
//Submit the approval request for processing
Approval.ProcessResult result = Approval.process(req);
}
//Single Approver
else if (loanapp.Approver1__r.Id!=null && loanapp.Approver2__r.Id==null && loanapp.Approver3__r.Id==null && loanapp.Approver4__r.Id==null){
Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
req.setObjectId(loanapp.Id);
//Submit the approval request for processing
Approval.ProcessResult result = Approval.process(req);
}
//Double Approver
else if (loanapp.Approver1__r.Id!=null && loanapp.Approver2__r.Id!=null && loanapp.Approver3__r.Id==null && loanapp.Approver4__r.Id==null){
Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
req.setObjectId(loanapp.Id);
//Submit the approval request for processing
Approval.ProcessResult result = Approval.process(req);
}
//Triple Approver
else if (loanapp.Approver1__r.Id!=null && loanapp.Approver2__r.Id!=null && loanapp.Approver3__r.Id!=null && loanapp.Approver4__r.Id==null){
Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
req.setObjectId(loanapp.Id);
//Submit the approval request for processing
Approval.ProcessResult result = Approval.process(req);
}
//Quadruple Approver
else if (loanapp.Approver1__r.Id!=null && loanapp.Approver2__r.Id!=null && loanapp.Approver3__r.Id!=null && loanapp.Approver4__r.Id!=null){
Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
req.setObjectId(loanapp.Id);
//Submit the approval request for processing
Approval.ProcessResult result = Approval.process(req);
}
}
}
}
<messaging:emailTemplate subject="Loan application awaiting your approval" recipientType="User" relatedToType="Loan_Application__c">
<messaging:HtmlEmailBody >
The following loan application has been submitted for your approval:<br/><br/>
Application ID: {!Relatedto.ID_Number__c}<br/>
Application Date: {!Relatedto.Date__c}<br/>
Applicant Name: {!Relatedto.Name__c}<br/>
Applicant Address: {!Relatedto.Address__c}<br/>
Application Credit Score: {!Relatedto.Credit_Score__c}<br/>
Application Credit History: {!Relatedto.Credit_History__c}<br/>
Application Notes: {!Relatedto.Notes__c}<br/><br/>
Please submit your response at your next earliest convenience-thank you.
</messaging:HtmlEmailBody>
</messaging:emailTemplate>