+ Start a Discussion
Support 3743Support 3743 

What do the components of apex:repeat mean and how do I write a Visual force email template?

Hi everyone, I'm a complete newbie to Apex and I'm recently writing an email template that sends out all of the opportunities related to a specific account name. However I don't fully understand what <apex:repeat> does.

In <apex:repeat var="op" value="{!relatedTo.Name}">, what does "var" refer to and what does "value" refer to? Does this create a loop that iterates through all of the records in relatedTo that have Name equal to value? 

Please help me with this! Thank you so much!
 
Best Answer chosen by Support 3743
Rahul.MishraRahul.Mishra
Hi,

You can assume that:  apex:repeat as a for loop in programming which iterates over the list of values (value="{!relatedTo.Name}", iterates over name coming from relatedTo list), var is same like loop veriable to iterate the list one by one, below is the simple example of iterating the list of account and printing the name of each account:
 
<apex:page standardController="Account" recordSetVar="accounts" tabstyle="account" sidebar="false">
    <apex:pageBlock >
          <apex:repeat value="{!accounts}" var="a">
    
          "{!a.name}"

</apex:repeat>      
     </apex:pageBlock>
</apex:page>
In value="{!accounts}", accounts is a list of account record which we are iterating through a.

Mark solved if it does help you.

All Answers

Rahul.MishraRahul.Mishra
Hi,

You can assume that:  apex:repeat as a for loop in programming which iterates over the list of values (value="{!relatedTo.Name}", iterates over name coming from relatedTo list), var is same like loop veriable to iterate the list one by one, below is the simple example of iterating the list of account and printing the name of each account:
 
<apex:page standardController="Account" recordSetVar="accounts" tabstyle="account" sidebar="false">
    <apex:pageBlock >
          <apex:repeat value="{!accounts}" var="a">
    
          "{!a.name}"

</apex:repeat>      
     </apex:pageBlock>
</apex:page>
In value="{!accounts}", accounts is a list of account record which we are iterating through a.

Mark solved if it does help you.
This was selected as the best answer
Support 3743Support 3743
@Rahul.Mishra. Great explanation, thank you! I have one more question: how could I view the parent-children relationship of objects? For example, I know that the child relationship field on Contact for Opportunity is Opportunities, but I have no idea where to find this relationship. 
Rahul.MishraRahul.Mishra
Hi,

You can find the name on field it-self. Like : Contact is related to account, so contact will have lookup field AccountId, when you click on it, you will get the relationship field name. For custom fields, relationship name is usally appended by __r.

Screenshot of relationship field on Contact:

User-added image

Mark solved if it does help you.