+ Start a Discussion
TN AdminTN Admin 

How to get the opportunity product fields in visualforce email template?

How to get the opportunity product fields in visualforce email template?

I have created VF email template relatedTo Contract__C object

Relationship:
Opportunity object---Lookup--->Contract__C object

Now hot to get the opportunity products fields on VF email template?..
 
Best Answer chosen by TN Admin
yogesh_sharmayogesh_sharma

Hello,

In this scenario, you can use controller with email template. Following are the steps you have to follow to achieve this functionality:

1. Create Visualforce component with controller

2. Write Apex repeat logic in your visualforce component (Display Opportunity Products)

3. Create Email template and use visualforce component in your email template with following tag:

<c.VFComponentName opportunityIDForController="{!relatedTo.OpportunityId}"/>


4. You can access this ID in your controller with following tag:

<apex:attribute id="oppId" assignTo="{!oppId}" name="opportunityIDForController" type="ID" description="The Opporunity's SF ID"/>


5. Here oppId is a get set variable, you can use this to Query on Opportunity Line Items to fetch all products and can use those product list in Apex repeat.

I hope it helps you to acheive your requirement. I am not sending you to full code, because I want you to first try it your self.

Please mark this as best answer, if it helps you, so that this solution would be easily searchable by other users.

Thanks,

Yogesh Sharma

All Answers

Naveen DhanarajNaveen Dhanaraj
Try Like this,
<apex:repeat value="{!relatedTo.OpportunityLineItems}" var="oli">
      <apex:outputText value="{!oli.PriceboonEntry.Product2.Name}">
      <apex:outputText value="{!oli.UnitPrice}">
      <apex:outputText value="{!oli.Quantity}">
</apex:repeat>

 
TN AdminTN Admin
But how can use Opportunity lineitems directly. there is no relation b/w lineitems and contract objects right?
yogesh_sharmayogesh_sharma

Hello,

As per my understanding you have following relations:

1. Parent object: Contract__c

2. Child: Opportunity

And you want to fetch Opportunity product in your visualforce email template. If requirement is the same, then you cannot fetch Opportunity Line Item in visualfore page email template by using Apex repeat. Because Opportunity Line Item is a sub-child of Contract__c and you cannot traverse more than 1 level in aggregate function.

Solutions:

To achieve this functionality, you have to use controller with visualforce Email template. Create a visualforce component with Controller and use that component in your visualforce email tyemplate. From Visualforce email template, you can pass Contract__C ID to component controller to fetch all Opportunities and related products.

I hope it helps you to achieve this functionality.

Please mark this answer as best if it helps you, so that it will be easy to search for other users.

Thanks,

Yogesh Sharma

TN AdminTN Admin
@yogesh, Parent object: Opportunity
                  Child: Contract__C

can you please help me how the code look like for both email template and controller...

Thanks in advance.... 
yogesh_sharmayogesh_sharma

Hello,

In this scenario, you can use controller with email template. Following are the steps you have to follow to achieve this functionality:

1. Create Visualforce component with controller

2. Write Apex repeat logic in your visualforce component (Display Opportunity Products)

3. Create Email template and use visualforce component in your email template with following tag:

<c.VFComponentName opportunityIDForController="{!relatedTo.OpportunityId}"/>


4. You can access this ID in your controller with following tag:

<apex:attribute id="oppId" assignTo="{!oppId}" name="opportunityIDForController" type="ID" description="The Opporunity's SF ID"/>


5. Here oppId is a get set variable, you can use this to Query on Opportunity Line Items to fetch all products and can use those product list in Apex repeat.

I hope it helps you to acheive your requirement. I am not sending you to full code, because I want you to first try it your self.

Please mark this as best answer, if it helps you, so that this solution would be easily searchable by other users.

Thanks,

Yogesh Sharma

This was selected as the best answer
TN AdminTN Admin
Is it possible to acheive this functionality without writing any controller?
yogesh_sharmayogesh_sharma

No. You have to write controller for this.

Thanks,

Yogesh Sharma