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
LaurenP6777LaurenP6777 

Visualforce Email Template with Multiple Related Records

Hi Everyone, 

I have written many VS email template that include Opportunities and Opportunity Line Items. However, I am struggling when it comes to writing a VS email template that includes Opportunity, Opportunity Line Items, and Contact Role. 

Does anyone have a sample VS email and Apex Controller Component that would accomplish something similar? All I have been able to find online is example where they are querying the entire system in the controller (i.e. All acounts that have the word "Smith"). That is not what i am looking for- I am looking to query based on a records related to a SPECIFIC Opportunity. 

Any help would be greatly appreciated. 
David OvellaDavid Ovella
I am using apex,email template and visual component


My Apex
public class OTPresupuestoControllers {
   
    public List<cOT> cOTList{get;set;} 
    public String otId;
    
    
    //Constructor
    public OTPresupuestoControllers() {    
 
    }
    
	//OT ID
    public String getOtId(){ 
        return otId;
    }
    
    public void setOtId(string OTId) {    
        otId = OTId;  
        this.cOTList = new List<cOT>();
        for(OT__c o : [SELECT Id, Name,Usuario_Envio__c,Nombre_del_Cliente_ppto__c, Total_a_Pagar__c, Descuento__c
          
From OT__c Where Id=:otId Limit 1])	{cOTList.add(new cOT(o));}  
        
 }

  
    public class cOT{
        public OT__c OT{get; set;}
        public String totalapagar{get;set;}
        public String saldoapagar{get;set;}

        public cOT(OT__c o){
            this.OT = o;
            this.totalapagar= NumberToSpanishWords.convertToGs(o.Total_a_Pagar__c);
            this.saldoapagar= NumberToSpanishWords.convertToGs(o.Saldo_a_Pagar__c);
            }      
    }    
}

my visual component
<!--Envio_Email_Componente-->
<apex:component controller="OTPresupuestoControllers" access="global" rendered="true"> 
    
    <apex:attribute name="opptyId" assignTo="{!otId}" type="String" description="Id OT__c"/>
    <apex:repeat var="ot" value="{!cOTList}">
      
   
    <!--CUERPO-->    
    <table> 
        <tr>
            <td>           
                <b><apex:outputText value="Nuestra Ref.:"/>&nbsp;
            	<apex:outputText value="{!ot.OT.Name}" style="color:#C11010;"/></b> 
            </td>
        </tr>                 
        <tr>     
            <td>
                <b>
                <apex:outputText value="Su Contacto: {!ot.OT.Usuario_Envio__c}"/>
                </b>      
            </td> 
        </tr>   
        <tr>  
            <td>
                <b>
                <apex:outputText style="font-size: 15px;" value="Total a Pagar:{!ot.totalapagar}"/>
                </b>          
            </td>
        </tr>        
        <tr> 
            <td>
                <apex:outputText value="Nombre o Razón Social: {!ot.OT.Nombre_de_la_Cuenta__c}"/>
            </td>
        </tr>     
    </table>
    </apex:repeat>
</apex:component>

my Visualforce Email Template
 
<messaging:emailTemplate subject="{!relatedTo.Asunto_email__c}" recipientType="User" relatedToType="OT__c">
<messaging:htmlEmailBody >
        <c:Envio_Email_Componente opptyId="{!relatedTo.Id}"/>
</messaging:htmlEmailBody>
</messaging:emailTemplate>