+ Start a Discussion
SainSain 

What is apex:repeat component is salesforce?

Hi,

Please any one can explain me about apex:repeat component in salesforce? where we can use?

regards,
Sain
KaranrajKaranraj
Sain - In simple its an iteration compoenent like an for statement in the OOPs concepts. An iteration component that allows you to output the contents of a collection according to a structure that you specify. The collection can include up to 1,000 items.
<!-- Page: -->

<apex:page controller="repeatCon" id="thePage">

    <apex:repeat value="{!strings}" var="string" id="theRepeat">

        <apex:outputText value="{!string}" id="theValue"/><br/>

    </apex:repeat>

</apex:page>


/*** Controller: ***/

public class repeatCon {

    public String[] getStrings() {
        return new String[]{'ONE','TWO','THREE'};
    }

}

Check this link for the more details - https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_repeat.htm
sfdc550sfdc550
<apex:repeat>:

An iteration component that allows you to output the contents of a collection according to a structure that you specify. The collection can include up to 1,000 items.

It is very simple, just use repeat in visualforce page.
Here is visualforce page and controller
 
It is very simple, just use repeat in visualforce page.
Here is visualforce page and controller

<apex:page controller="UseofRepeatOnAccountController" sidebar="false" tabStyle="Account">
    <apex:form >
    <apex:pageBlock title="Accounts with assoicated Contacts">
        <apex:repeat value="{!accountList }" var="acc">
            <apex:pageBlockSection title="{!acc.name}">
                <apex:pageBlockTable value="{!acc.contacts}" var="con">
                    <apex:column value="{!con.Firstname}"/>
                    <apex:column value="{!con.Lastname}"/>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:repeat>
    </apex:pageBlock>
    </apex:form>
</apex:page>

Controller is..
public class UseofRepeatOnAccountController {
    public List<Account> accountList{get;set;}
   
    public UseofRepeatOnAccountController() {
        accountList = new List<Account>();
        accountList = [SELECT id, Name,(SELECT LastName ,FirstName From Contacts)From Account limit 5] ;
    }
}



 
Amit Chaudhary 8Amit Chaudhary 8
apex:repeat
An iteration component that allows you to output the contents of a collection according to a structure that you specify. The collection can include up to 1,000 items
Please check below post for more detail on same
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_repeat.htm
http://www.thephani.com/difference-between-pageblocktabe-datatable-repeat/
Please try below smiple code for repeat. I hope that wll help u
<apex:page standardController="Account" recordSetVar="accounts" tabstyle="account" sidebar="false">
    <apex:pageBlock >
          <apex:repeat value="{!accounts}" var="a" >
                <apex:pageBlockSection title="{!a.name}" columns="1">
                      <apex:relatedList list="Contacts" subject="{!a.Id}"/>
                </apex:pageBlockSection>
            </apex:repeat>      
         </apex:pageBlock>
</apex:page>
Please let us know if this will help u

Thanks
Amit Chaudhary


 
SainSain
Thanks Karanraj, amit for ur support.
PlatFormCloudPlatFormCloud
Hi Amit,
We need to fetch related list records associated with the Account. Then I think your code will not works properly. Please check.
Amit Chaudhary 8Amit Chaudhary 8
Hi PlatFormCloud,

Above example was just for apex:repeat tag. If you want to see related list for account try below code.
<apex:page standardController="Account" recordSetVar="accounts" tabstyle="account" sidebar="false">
    <apex:pageBlock >
          <apex:repeat value="{!accounts}" var="a">
<apex:pageBlockSection title="{!a.name}"></apex:pageBlockSection>
  <apex:relatedList list="Contacts" subject="{!a.Id}"/>
<apex:relatedList list="Opportunities" subject="{!a.Id}" />
</apex:repeat>      
     </apex:pageBlock>
</apex:page>


 
Lawrence PrivCoLawrence PrivCo
I am confused. Where does {!strings} come from? This is strange:

      <apex:repeat value="{!strings}" var="string" id="theRepeat">

The controller has no variable called "strings", so how can we use a variable called {strings}. Where does the view get this variable called {strings}? I would have expected {!getStrings}.

public class repeatCon {
    public String[] getStrings() {
        return new String[]{'ONE','TWO','THREE'};
    }
}
SScholtzSScholtz
Hey Lawrence,

Apex is a derivative of java.  I'm not a java guy, so I probably won't explain this well or use the right words, but public variables in your controller that you intend to display or manipulate should have getters - something that returns the value - and setters - something that sets the value.

They can be simple...
public String something { get; set; }

...a little complex...
public String something {
  get {
    return "You got something!";
  }
  set {
    something = value; // "value" is just an automatic thing that you get using this structure
  }
}

...or you can use methods and skip defining the variable at all:
public String getSomething() {
  return "You got something!";
}

// I believe "set" will only work if you actually have a variable to set,
// while "get" will work without having the variable

public String something; // no get or set defined
public void setSomething(String userInput) {
  this.something = userInput;
}

If you reference {!something} in your VF page, Salesforce either looks for the variable "something" and it's "get" definition, or it will just assume and look for a method called "getSomething()".  If it can't find either you'll get a compile (can't save to the server) or runtime error (VF page will render an error), can't remember which.

This post might also help: http://www.forcetree.com/2009/07/getter-and-setter-methods-what-are-they.html



 
Ankush DurejaAnkush Dureja
apex:repeat is an iteration component that allows you to output the contents of a collection according to a structure that you specify. The collection can include up to 1,000 items.
 
<apex:page controller="repeatCon" id="thePage">
    <apex:repeat value="{!strings}" var="string" id="theRepeat">
        <apex:outputText value="{!string}" id="theValue"/><br/>
    </apex:repeat>
</apex:page>

For more details, check below link:
http://www.sfdcpoint.com/salesforce/using-apexrepeat-in-visualforce-page/
Mohit MangalMohit Mangal
creating the pdf code.

1.open the salesforce.com
2.go to lighting experience
3.go to developer console 
4.create new->visualforce page
5.name:-createpdf
copy this code

<apex:page standardController="Account" recordSetVar="acc" renderAs="pdf">
    <apex:form >
        <center><img src="{!$Resource.img}" width="300" height="100" /></center>
        <h1 style="font-size:17px;">Innowise pvt ltd</h1>
        <p style="font-family:Arial, Gadget, sans-serif; font-size:12px;">103,Suratwala Mark Plazzo,Wakad-Hinjewadi Road,pune,Maharashtra 411057</p>
        <body>
            <div>
            <table style="border-collapse:collapse; width:100%; font-family:rebuchet MS,Arial,Helvetica,sans-serif;">
                <tbody style="display:table-row-group;vertical-align:middle;">
                <tr style="display:table-row;">
                    <th style="font-size:1 em;text-align:left;padding-top:5px;padding-bottom:4px;padding:3px 7px 2px 7px; background-color:#A7C942; color:#fff; border:1px solid #98bf21;">Company Name</th>
                    <th style="font-size:1 em;text-align:left;padding-top:5px;padding-bottom:4px;padding:3px 7px 2px 7px; background-color:#A7C942; color:#fff; border:1px solid #98bf21;">Contact Number</th>
                    <th style="font-size:1 em;text-align:left;padding-top:5px;padding-bottom:4px;padding:3px 7px 2px 7px; background-color:#A7C942; color:#fff; border:1px solid #98bf21;">Company Type</th>
                </tr>
                    
                <apex:repeat value="{!acc}" var="a" >
                    <tr>
                        <td style="font-size:1em;border:1px solid #98bf21;padding:3px 7px 2px 7px;">{!a.Name}</td>
                        <td style="font-size:1em;border:1px solid #98bf21;padding:3px 7px 2px 7px;">{!a.Phone}</td>
                        <td style="font-size:1em;border:1px solid #98bf21;padding:3px 7px 2px 7px;">{!a.Type}</td>
                    </tr>
                </apex:repeat>
                </tbody>
            </table>
            </div>
            <div style="padding-top:30px;line-height:24px;">
                <h8 style="font-size:17px;">Thanks and Regards</h8><br/>
                <span style="font-size:17px;font-weight:bold;font-family:Britannic Bold,serif;color:#92D050">Innowise</span>
                <span style="font-size:17px;font-weight:bold;font-family:Britannic Bold,serif;color:#00B0F0">Team</span>   
            </div>
        </body>
    </apex:form>
</apex:page>
Mohit MangalMohit Mangal
this is my Apex Class 
@RestResource(urlMapping='/showduplicate/*')
global class accountusingurl 
{
public List<AggregateResult> cs{get;set;}
@HttpGet
global static List<AggregateResult> accounturl()
    {
        
       List<AggregateResult> cs=new list<AggregateResult>();
        try
    {
       for(AggregateResult aggres :[SELECT count(Id) mycount,Prospect_Code__c,Name FROM Account where Id Not In (Select AccountId from Contact)
                         And Id Not In (Select AccountId from Opportunity) GROUP BY Name, Prospect_Code__c HAVING count(Id)>1 ])
       {
            cs.add(aggres);
           return cs ;
         
       }
    }
    catch(Exception ex)
    {
        system.debug('Error'+ex.getMessage());
    }
        return cs;

 }
}

when i run at workbench its shown like this

User-added image

I want to get the proper name id in json format how i do it..?