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
aparna d 1aparna d 1 

passing record id to email templates using visualforce components

Hi All,

I am passing record id to email tempalte but am not getting values from visual component to eamil template . Below is my code.
Please help on this. Thanks in advance.
 
Apex class:
public class csCompetitiveOppNotification {
    
    public Id OppObj{get;set;}
    public Opportunity opp{get;set;} 
    public string Competitive_Infomation{get;set;}
    public string SubOppName{get;set;}
    
    public csCompetitiveOppNotification(){
        try{
            Competitive_Infomation='';
            SubOppName='';
            opp=[Select Id,name,StageName,Competitive__c,Region__c,Territory__c,Ownerid,End_User__c,Accountid,UnitCount_Product__c,Line_Item_PO_Contract_Value__c,Revenue__c from opportunity where id=:OppObj];
            List<Opportunity> subOppList=  [select name from opportunity where Opportunity_Master__c=:OppObj];
            for(Opportunity oppObj:subOppList)    {
                if(SubOppName !=''){
                    SubOppName=SubOppName+', ';
                }
                SubOppName=SubOppName+oppObj.name;
                
            }        
            List<Competitive_Infomation__c> comList=[select name from Competitive_Infomation__c where Opportunity__c=:OppObj];
            for(Competitive_Infomation__c ciObj:comList){
                if(Competitive_Infomation !=''){
                    Competitive_Infomation=Competitive_Infomation+', ';
                }
                Competitive_Infomation=Competitive_Infomation+ciObj.name;
            }   
        }
        catch(exception e){
            system.debug('test'+e.getMessage());
        }
    }
}
 
VF component:
<apex:component controller="csCompetitiveOppNotification" access="global">
    <apex:attribute name="Oppid" type="Id" assignTo="{!OppObj}" description="This is the Id of the Opportunity" access="global" />
    
    <apex:outputPanel layout="block">
        <html>
            <body>
                
                <table> 
                    <tr> 
                        <td style="font-weight:bold">Master Opportunity Name:</td>
                        <td>{!opp.Name}</td>
                    </tr>    
                    
                    <tr> 
                        <td style="font-weight:bold">Account Name:</td>
                        <td>{!opp.Account.Name}</td>
                    </tr>   
                    <tr> 
                        <td style="font-weight:bold">Enduser:</td>
                        <td>{!opp.End_User__c }</td>
                    </tr> 
                    
                    <tr> 
                        <td style="font-weight:bold">Competitive Information:</td>
                        <td>{!Competitive_Infomation}</td>
                    </tr>   
                    <tr> 
                        <td style="font-weight:bold">Sub Opportunities:</td>  
                        <td>{!SubOppName }</td>
                    </tr> 
                    
                    
                    
                    
                </table>
            </body>               
        </html>
    </apex:outputPanel>
</apex:component>
Template
<messaging:emailTemplate subject="Competitive Opportunity {!RelatedTo.Inquiry_Number__c} -{!RelatedTo.Name} - {!RelatedTo.Territory__c}  {!Relatedto.id}" recipientType="User" relatedToType="Opportunity">
<messaging:htmlEmailBody >
<c:cs_Comp_Opp_Notification Oppid="{!Relatedto.Id}"  >
</c:cs_Comp_Opp_Notification>
</messaging:htmlEmailBody>
</messaging:emailTemplate>

please anyone give me solution on this.
 
Suraj Tripathi 47Suraj Tripathi 47
Hi, Aparna d 1,

"Try this one, I used this for getting the record Id of the current record page."
Apex Controller:
public class CurrentRecordIdController {
public String currentId {get;set;}
public String parameterValue {get;set;}
public SObject acc{get;set;}
 
    public Sobject getopptys(ApexPages.StandardController controller) {
        currentId  = ApexPages.CurrentPage().getparameters().get('id');
        SObject acc = Database.query('select id from SObject where id =: currentId');
        parameterValue = ApexPages.CurrentPage().getparameters().get('namePa');
        return acc;
    }
}

Component:

Name: FetchId
 
<apex:component controller="CurrentRecordIdController" access="global">
    <apex:attribute name="AcctId" type="Id" description="Id of the account" assignTo="{!currentId}"/>       
</apex:component>

Visualforce Email template:
<messaging:emailTemplate subject="List of Account" recipientType="User" relatedToType="Account">
    <messaging:htmlEmailBody >
    Hi,<br/>
    Name {!relatedTo.Name}.<br/><br/>
    <c:FetchId AcctId="{!relatedTo.Id}" /><br/><br/>
    <b>Regards,</b><br/>
    {!recipient.FirstName}
    </messaging:htmlEmailBody>
</messaging:emailTemplate>


If you find your Solution then mark this as the best answer. 

Thank you!

Regards 
Suraj Tripathi