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
Jill HedbergJill Hedberg 

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?

Best Answer chosen by Admin (Salesforce Developers) 
Jill HedbergJill Hedberg

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

imutsavimutsav
You should use if condition to check if the Contact Role is primary or not something like this :
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.]


venkateshyadav1243venkateshyadav1243

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 });
        
 }
      
}

Jill HedbergJill Hedberg

I am not sure how to write the IF condition and where in my code I should put it. Could you clarify?

imutsavimutsav
Hi Jill,

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.]
Jill HedbergJill Hedberg

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>

This was selected as the best answer
imutsavimutsav
Oh sorry missed that part anyways good you figured that out.
Mitchell LarsenMitchell Larsen
I'm wondering if anyone can help me with a related question here. I want to do the same thing, but I don't want to filter by primary, but rather by role=customer only. I also would ideally like to add a line break in between each contact. So - basically put a list of all customers into the template.