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
David OvellaDavid Ovella 

how to connect the ID in this apex?

Hello, I'm trying to send emails with information from a custom object called OT__c, but when I receive the email has fields in blank and I think is because I'm not connecting correctly the Id of OT__c in apex to the component to the email template.

my email template
<messaging:emailTemplate subject="RE: {!relatedTo.Asunto_email__c}" recipientType="User" relatedToType="OT__c">
<messaging:htmlEmailBody >
        <br/>
        <b><u>MAKER S.A</u></b><br/>
        <b>Nuestra Ref.:</b> &nbsp; <apex:outputText value="{!relatedTo.Name}"/><br/>
        <br/>
        <c:Envio_Email_Componente opptyId="{!relatedTo.Id}"/>
            
</messaging:htmlEmailBody>
</messaging:emailTemplate>



my component
<apex:component controller="OTPresupuestoControllers" access="global" rendered="true">
    <apex:attribute name="opptyId" assignTo="{!otId}" type="String" description="Id OT__c"/>    
     
    <!--just to see if the variable is working-->
     {!otId} {!opptyId}
    
   
    <apex:repeat var="ot" value="{!cOTList}">      
        <div style="font-size:18px;">                    
            <b>Item 1</b>   
        </div>   
        <div style="font-size: 14px;">               	
            <b>Nombre:</b> <apex:outputText value=" {!ot.OT.Nombre_de_la_Cuenta__c}" />     
        </div>
        <div style="font-size: 14px;">               	
            <u>Total Gs.:</u> <apex:outputText value=" {!ot.total1} IVA incl."/>     
        </div>   
        <div style="font-size: 14px;">               		
            <b><apex:outputText value="Total a Pagar Gs.: {!ot.totalapagar} IVA incl."/></b>               
        </div>
        <br/>
        <div style="font-size: 14px;">               	
            <b>Archivo:</b> <apex:outputText value=" {!ot.OT.Archivo__c}"/>    
        </div>          
        <div style="font-size: 14px;">               	
            <b>Plazo de Entrega:</b> <apex:outputText value=" {!ot.OT.Plazo_de_Entrega__c}hs"/>     
        </div>            
        <div style="font-size: 14px;">               	
            <b>Forma de Pago:</b> <apex:outputText value=" {!ot.OT.Forma_de_Pago__c}"/>     
        </div>       
        <div style="font-size: 14px;">               	
            <b>Requiere Seña:</b> <apex:outputText value=" {!ot.OT.Requiere_Sena__c}" />    
        </div>          
    </apex:repeat>   
</apex:component>


my apex
public class OTPresupuestoControllers {
  
    public List<cOT> cOTList{get;set;}
    public String otId {get;set;} //Don't know if this is the correct code

    public OTPresupuestoControllers() {    
        
        //OT List
        this.cOTList = new List<cOT>();
        for(OT__c o : [SELECT Id, Total_1__c, Total_a_Pagar__c, Archivo__c, Plazo_de_Entrega__c,
                       Forma_de_Pago__c, Requiere_Sena__c, Nombre_de_la_cuenta__c
                       From OT__c Limit 1]) //it's not working
        {
            cOTList.add(new cOT(o));
        }                 
        
    }
      
    
    public class cOT{
        public OT__c OT{get; set;}
        public String total1{get;set;}
        public String totalapagar{get;set;}

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



 
Best Answer chosen by David Ovella
JeffreyStevensJeffreyStevens
So, you're saying your otID is null?

You know - I had this exact same problem.  I'm not sure if this is the correct way to handle it, but I solved it like this.  

I created a get and a set method for the ID property. And then I put all the code I needed to get my info in the Setter of the last attribute.  I think this is because the getter/setters are called before the constuctor.

So, I think some code like this would fix your issue...
 
public string otId;

public string getOtId() { return otId }
public void setOtId(string inString) {
    otId = inString;
    cOTList = [SELECT id,Total_1__c, ....
                          FROM OT__c
                          LIMIT 1];
}

Maybe someone else could confirm why?

All Answers

JeffreyStevensJeffreyStevens
So, you're saying your otID is null?

You know - I had this exact same problem.  I'm not sure if this is the correct way to handle it, but I solved it like this.  

I created a get and a set method for the ID property. And then I put all the code I needed to get my info in the Setter of the last attribute.  I think this is because the getter/setters are called before the constuctor.

So, I think some code like this would fix your issue...
 
public string otId;

public string getOtId() { return otId }
public void setOtId(string inString) {
    otId = inString;
    cOTList = [SELECT id,Total_1__c, ....
                          FROM OT__c
                          LIMIT 1];
}

Maybe someone else could confirm why?
This was selected as the best answer
David OvellaDavid Ovella
I was weeks trying to resolve this in differents ways but nothing worked and now it works perfectly. I receive the email with the correct values.
​thanks Jeffrey