+ Start a Discussion
jutamindjutamind 

Retrieve primary contact role from within VF email template

Hi,

 

I've created a simple VF email template based on the solution found in this forum. However, i found out that the labels are printed many times if there are a few contact roles associated with an opportunity. How do i modify my codes to print the labels of my primary contact role?

 

Appreciate some help. Thanks.

 

My VF codes as below:

 

<messaging:emailTemplate subject="Call Hot Lead" recipientType="User" relatedToType="Opportunity">
<messaging:plainTextEmailBody >

Account: {!Relatedto.Account.Name}
Opportunity Name: {!Relatedto.Name}
<apex:repeat value="{!RelatedTo.OpportunityContactRoles}" var="ContactRole">
Name: <apex:outputText value="{!ContactRole.Contact.Salutation}" rendered="{!AND(ContactRole.isPrimary)}"></apex:outputtext> <apex:outputText value="{!ContactRole.Contact.Name}" rendered="{!AND(ContactRole.isPrimary)}"></apex:outputtext>
Title: <apex:outputText value="{!ContactRole.Contact.Title}" rendered="{!AND(ContactRole.isPrimary)}"></apex:outputtext>
Workplace: <apex:outputText value="{!ContactRole.Contact.Workplace__c}" rendered="{!AND(ContactRole.isPrimary)}"></apex:outputtext>
Contact Status: <apex:outputText value="{!ContactRole.Contact.Contact_Status__c}" rendered="{!AND(ContactRole.isPrimary)}"></apex:outputtext>
Source: <apex:outputText value="{!ContactRole.Contact.Source__c}" rendered="{!AND(ContactRole.isPrimary)}"></apex:outputtext>
Direct Phone: <apex:outputText value="{!ContactRole.Contact.Direct_Phone__c}" rendered="{!AND(ContactRole.isPrimary)}"></apex:outputtext>
Ext: <apex:outputText value="{!ContactRole.Contact.Ext__c}" rendered="{!AND(ContactRole.isPrimary)}"></apex:outputtext>
Mobile: <apex:outputText value="{!ContactRole.Contact.MobilePhone}" rendered="{!AND(ContactRole.isPrimary)}"></apex:outputtext>
Email: <apex:outputText value="{!ContactRole.Contact.Email}" rendered="{!AND(ContactRole.isPrimary)}"></apex:outputtext>
</apex:repeat>

Detail: {!Relatedto.Description}
 
</messaging:plainTextEmailBody>
</messaging:emailTemplate>

Arjun SrivastavaArjun Srivastava

You can simply do like this :

<apex:repeat value="{!RelatedTo.OpportunityContactRoles}" var="ContactRole" rendered="{!RelatedTo.OpportunityContactRoles.isPrimary}">
Name: <apex:outputText value="{!ContactRole.Contact.Salutation}"></apex:outputtext> 
<apex:outputText value="{!ContactRole.Contact.Name}" ></apex:outputtext>
Title: <apex:outputText value="{!ContactRole.Contact.Title}" ></apex:outputtext>
Workplace: <apex:outputText value="{!ContactRole.Contact.Workplace__c}"></apex:outputtext>
Contact Status: <apex:outputText value="{!ContactRole.Contact.Contact_Status__c}" ></apex:outputtext>
Source: <apex:outputText value="{!ContactRole.Contact.Source__c}" ></apex:outputtext>
Direct Phone: <apex:outputText value="{!ContactRole.Contact.Direct_Phone__c}" ></apex:outputtext>
Ext: <apex:outputText value="{!ContactRole.Contact.Ext__c}" ></apex:outputtext>
Mobile: <apex:outputText value="{!ContactRole.Contact.MobilePhone}" ></apex:outputtext>
Email: <apex:outputText value="{!ContactRole.Contact.Email}" ></apex:outputtext>
</apex:repeat>

 No need to add rendered attribute inside each outputtext tag.Add it in Repeat only like above. This works fine for me.

 

 

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

jutamindjutamind

Hi stdc_star,

 

I've tried to modified my VF email template to be as what you've mentioned, but i get the error message: Error: Unknown property 'VisualforceArrayList.isPrimary'

 

Modified codes as below:

 

<messaging:emailTemplate subject="Call Hot Lead" recipientType="User" relatedToType="Opportunity">
<messaging:plainTextEmailBody >

Account: {!Relatedto.Account.Name}
Opportunity Name: {!Relatedto.Name}
<apex:repeat value="{!RelatedTo.OpportunityContactRoles}" var="ContactRole" rendered="{!RelatedTo.OpportunityContactRoles.isPrimary}">
Name: <apex:outputText value="{!ContactRole.Contact.Salutation}"></apex:outputtext> <apex:outputText value="{!ContactRole.Contact.Name}"></apex:outputtext>
Title: <apex:outputText value="{!ContactRole.Contact.Title}"></apex:outputtext>
Workplace: <apex:outputText value="{!ContactRole.Contact.Workplace__c}"></apex:outputtext>
Contact Status: <apex:outputText value="{!ContactRole.Contact.Contact_Status__c}"></apex:outputtext>
Source: <apex:outputText value="{!ContactRole.Contact.Source__c}"></apex:outputtext>
Direct Phone: <apex:outputText value="{!ContactRole.Contact.Direct_Phone__c}"></apex:outputtext>
Ext: <apex:outputText value="{!ContactRole.Contact.Ext__c}"></apex:outputtext>
Mobile: <apex:outputText value="{!ContactRole.Contact.MobilePhone}"></apex:outputtext>
Email: <apex:outputText value="{!ContactRole.Contact.Email}"></apex:outputtext>
</apex:repeat>
Detail: {!Relatedto.Description}
 
</messaging:plainTextEmailBody>
</messaging:emailTemplate>

jutamindjutamind

anyone else can help out?

Arjun SrivastavaArjun Srivastava
<messaging:emailTemplate subject="Call Hot Lead" recipientType="User" relatedToType="Opportunity">
<messaging:plainTextEmailBody >
<apex:repeat value="{!RelatedTo.OpportunityContactRoles}" var="ContactRole">
Name: <apex:outputText value="{!ContactRole.Contact.Salutation}" rendered="{!ContactRole.IsPrimary}"></apex:outputtext> 
<apex:outputText value="{!ContactRole.Contact.Name}" rendered="{!ContactRole.IsPrimary}" ></apex:outputtext>
Title: <apex:outputText value="{!ContactRole.Contact.Title}" rendered="{!ContactRole.IsPrimary}"></apex:outputtext>
Mobile: <apex:outputText value="{!ContactRole.Contact.MobilePhone}" rendered="{!ContactRole.IsPrimary}"></apex:outputtext>
Email: <apex:outputText value="{!ContactRole.Contact.Email}" rendered="{!ContactRole.IsPrimary}"></apex:outputtext>
</apex:repeat>
 
</messaging:plainTextEmailBody>
</messaging:emailTemplate>

This is working fine.Try this.

 

 If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

jutamindjutamind

the code you modified above works, if there's only 1 contact role. if there's multiple contact roles inside an opportunity, then you will see the labels such as Name, Title etc, printed many times over and over.