You need to sign in to do that
Don't have an account?

How do I get the primary contact from Contact Roles related list into my visualforce email template?
Hi!
I want to get the name, email address and phone number of the person marked as primary contact on the Contact Roles related list in the opportunity into my Visualforce email template.
I have created this code:
<apex:repeat value="{!RelatedTo.OpportunityContactRoles}" var="ContactRole">
{!ContactRole.Contact.Name}, {!ContactRole.Contact.Title}, {!ContactRole.Contact.Email}, {!ContactRole.Contact.Phone},
</apex:repeat>
but it lists ALL people in the Contact roles related list, and I just want the primary contact. How can I accomplish this?
I see, thanks for that info! This code almost worked, but with the NOT I got all contacts except for the primary :) Changed it to AND instead and it worked fine. Thank you!
This is my new code if anyone wants it sometime (decided to skip some info):
<p>
<b>Primary Contact:</b>
<apex:repeat value="{!RelatedTo.OpportunityContactRoles}" var="ContactRole">
<apex:outputText value="{!ContactRole.Contact.Name}" rendered="{!AND(ContactRole.isPrimary)}"></apex:outputtext>
<apex:outputText value="{!ContactRole.Contact.Phone}" rendered="{!AND(ContactRole.isPrimary)}"></apex:outputtext>
<apex:outputText value="{!ContactRole.Contact.Email}" rendered="{!AND(ContactRole.isPrimary)}"></apex:outputtext>
</apex:repeat>
</p>
All Answers
There is a field on Opportunity Contact Role isPrimary which will tell you if the contact role is primary or not.
{!IF(condition,'YES','NO')}
Thanks
Utsav
[Do mark this answer as solution if it works for you and give a kudos.]
Hi
I hope it will help you
Actulay in my scenario am sending the email for contact who contact role is primary
public class Sending_Email
{
public List<Opportunity> Opp=new List<Opportunity> ();
public List<contact> con=new List<contact>();
public List<OpportunityContactRole> ocr = new List<OpportunityContactRole>();
public set<id> ids=new set<id>();
public Opportunity opport;
public List<id> ocrids=new List<id>();
public void se(){
// getting the list of all opportunity where stage name is contracted performance
Opp=[select id,Name,StageName,Accountid from Opportunity where StageName='Contracted Performance' and ];
system.debug('#####'+opp);
for(Opportunity op:opp)
{
ids.add(op.id); //adding all the list opportunity in a set to get the ids
}
// getting the contract role where primary key is checked for all opprotunity
ocr=[select ContactId,Id,OpportunityId,IsPrimary from OpportunityContactRole where IsPrimary=true and OpportunityId in :ids];
system.debug('@@@@@@@@@@@'+ocr);
for(OpportunityContactRole oc:ocr)
{
system.debug('*********@@@@@@@');
ocrids.add(oc.ContactId); // getting the all contacts of emails,where the primary key is true
system.debug('*********@@@@@@@'+ocrids);
}
Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
mail.setTargetObjectIds(ocrids); // assign the all emails
//mail.setReplyTo('exemple@exemple.ex');
EmailTemplate et= [SELECT id FROM EmailTemplate WHERE developerName = 'Emails_Before_Event'];
mail.setSenderDisplayName('Incognito Artists'); // displaying the senders name
mail.setTemplateId(et.id); // adding the templete id,here we are taken subject,body.
Messaging.sendEmail(new Messaging.MassEmailMessage[] { mail });
}
}
I am not sure how to write the IF condition and where in my code I should put it. Could you clarify?
IF statement can be used anywhere in the VF page. However you can get this issue resolved by trying this.
<apex:repeat value="{!RelatedTo.OpportunityContactRoles}" var="ContactRole">
<apex:outputText value='{!ContactRole.Contact.Name}' rendered="{!NOT(ContactRole.isPrimary)}">
<apex:outputText value='{!ContactRole.Contact.Title}' rendered="{!NOT(ContactRole.isPrimary)}">
<apex:outputText value='{!ContactRole.Contact.Email}}}' rendered="{!NOT(ContactRole.isPrimary)}">
</apex:repeat>
Thanks
Utsav
[Do mark this answer as solution if it works for you and give a kudos.]
I see, thanks for that info! This code almost worked, but with the NOT I got all contacts except for the primary :) Changed it to AND instead and it worked fine. Thank you!
This is my new code if anyone wants it sometime (decided to skip some info):
<p>
<b>Primary Contact:</b>
<apex:repeat value="{!RelatedTo.OpportunityContactRoles}" var="ContactRole">
<apex:outputText value="{!ContactRole.Contact.Name}" rendered="{!AND(ContactRole.isPrimary)}"></apex:outputtext>
<apex:outputText value="{!ContactRole.Contact.Phone}" rendered="{!AND(ContactRole.isPrimary)}"></apex:outputtext>
<apex:outputText value="{!ContactRole.Contact.Email}" rendered="{!AND(ContactRole.isPrimary)}"></apex:outputtext>
</apex:repeat>
</p>