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
acrozieracrozier 

displaying list of contacts related to account

Hello,

 

I am trying to create a HTML email that goes out when a new contact is added.  I need this email to contain all the contacts related to the account of the contact just added.  Below is the code i am using which is only displaying the contact that is added and no other contacts from the account.  Any help would be greatly appreciated.

 

 

<messaging:emailTemplate subject="New Contact Created" recipientType="User" relatedToType="Contact">
<messaging:htmlEmailBody >  
<html>
        <body>
         <STYLE type="text/css">
               TH {font-size: 11px; font-face: arial;background: #CCCCCC; border-width: 1;  text-align: center } 
               TD  {font-size: 11px; font-face: verdana } 
               TABLE {border: solid #CCCCCC; border-width: 1}
               TR {border: solid #CCCCCC; border-width: 1}
         </STYLE>
         <font face="arial" size="3">

           <p>Dear Lisa,</p>
               <p>A new contact has been added, How excited are you? Below should be a list of related contacts, but probably not. <a href="https://na1.salesforce.com/{!relatedTo.accountid}">{!relatedTo.account.Name}.</a></p>
           <table border="0" >
                 <tr > 
                     <th>Action</th><th>Contact</th><th>Gift Code</th><th>Create Date</th><th>Create By</th>
                  </tr>
             <apex:repeat var="cx" value="{!relatedTo}">
               <tr>
                   <td><a href="https://na1.salesforce.com/{!cx.id}">View</a> |  
                   <a href="https://na1.salesforce.com/{!cx.id}/e">Edit</a></td>
                   <td>{!cx.Firstname} {!cx.Lastname}</td>
                   <td>{!cx.Gift_Code__c}</td>
                   <td>{!cx.createdDate}</td>
                   <td>{!cx.createdby.Firstname}</td>
                   
               </tr>
               
            </apex:repeat>            
           </table>
               <p />
         </font>
        </body>
    </html>
  </messaging:htmlEmailBody>  
</messaging:emailTemplate>

 

 

jkucerajkucera

I'm not an email expert but may be able to help.  What is the error or unexpected behavior you are seeing?

acrozieracrozier
There are no error messages but the only contact that is displayed is the one who was just added. All the other contacts in the account do not show up
CoryCowgillCoryCowgill

Try configuring your email template to relate to the Account object instead of Contact. Then you can access all the children object for the account.

 

This code will send you an email with list of contact on an account..

 

<messaging:emailTemplate subject="Test Email" recipientType="User" relatedToType="Account">
<messaging:plainTextEmailBody >
Account Name: {!relatedTo.Name}
<apex:repeat var="cx" value="{!relatedTo.Contacts}">
    {!cx.FirstName} {!cx.LastNAme} {!cx.Id}
</apex:repeat>
</messaging:plainTextEmailBody>
</messaging:emailTemplate>

acrozieracrozier

Thanks for you help, this makes sense.  I tried the code though and the emails are coming through blank.  I have a workflow rule set to send this email whenever a new contact is added.  Do you know what i could be missing?  None of the merge fields seem to be populating any data