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
sf.dev.ax1103sf.dev.ax1103 

Pagination in custom related list

Hi,

        I need to display related opportunities for first 5 records.if there are more than 5 records i need a link to show more records in next page.I need similar pagination how default salesforce provides for a related list.Can you please help me in this.

 

 

<apex:page standardController="Contact" extensions="sampleDetailPageCon">
<style>
.fewerMore { display: none;}
</style>
<apex:form >
<apex:pageMessages />
<apex:detail relatedList="true"></apex:detail>
<apex:pageblock id="CustomList" title="Related Opportunities" >
<apex:pageBlockTable value="{!oppz}" var="o" rendered="{!NOT(ISNULL(oppz))}">
<apex:column value="{!o.Name}"/>
<apex:column value="{!o.Account.Name}"/>
<apex:column value="{!o.Type}"/>
<apex:column value="{!o.Amount}"></apex:column>
<apex:column value="{!o.CloseDate}"/>
</apex:pageBlockTable>
<apex:outputLabel value="No records to display" rendered="{!(ISNULL(oppz))}" styleClass="noRowsHeader"></apex:outputLabel>
</apex:pageblock>
</apex:form>
</apex:page>



public class sampleDetailPageCon {
private List<Opportunity> oppz;
private Contact cntact;
public sampleDetailPageCon(ApexPages.StandardController controller) {
this.cntact= (Contact)controller.getRecord();
}
public List<Opportunity> getOppz()
{
Contact con = [Select id, Account.id FROM Contact where id = :cntact.id];
if (con.Account == null)
return null;
oppz = [Select id, Name, Account.Name, CloseDate, Amount, Type from Opportunity where Account.id = :con.Account.id];
return oppz;
}
}



  Thanks



SidharthSidharth

check out this exmaple:

 

<apex:page standardController="Account" recordSetvar="accounts" extensions="test_pagination">
	<apex:pageBlock title="Viewing Accounts">	
	<apex:form id="theForm">
	
		<apex:pageBlockSection >	
	        <apex:pageBlockTable value="{!test_pagination}" var="a">
          		<apex:column value="{!a.Original_Creditor__c}" />
        	</apex:pageBlockTable>       
		</apex:pageBlockSection>	
		
		<apex:panelGrid columns="4">
    		<apex:commandLink action="{!first}">First</apex:commandlink>
    		<apex:commandLink action="{!previous}" rendered="{!hasPrevious}">Previous</apex:commandlink>
    		<apex:commandLink action="{!next}" rendered="{!hasNext}">Next</apex:commandlink>
    		<apex:commandLink action="{!last}">Last</apex:commandlink>
		</apex:panelGrid>
		
	</apex:form>
	</apex:pageBlock>
</apex:page>

 

public with sharing class test_pagination {
  
  private final Account acct;
  private List<Accounts__c> accs;

  public test_pagination(ApexPages.StandardSetController controller) {
    this.acct = (Account)controller.getRecord();
  }

  public ApexPages.StandardSetController accountRecords{
    get {
      if(accountRecords == null) {
        accountRecords = new ApexPages.StandardSetController(Database.getQueryLocator( [Select id, Name, Original_Creditor__c from Accounts__c where Client__r.Id =
                        :ApexPages.currentPage().getParameters().get('id') order By id limit 20 ]));
                accountRecords.setPageSize(5);
      }
    return accountRecords;
    }
  private set;
  }
  
  public List<Accounts__c> gettest_pagination() {
    accs = (List<Accounts__c>) accountRecords.getRecords();
    return accs;
  }
  
  public Boolean hasNext {
    get {
      return accountRecords.getHasNext();
    }
    set;
  }

  public Boolean hasPrevious {
    get {
      return accountRecords.getHasPrevious();
    }
    set;
  }

   public void first() {
     accountRecords.first();
   }

   public void last() {
     accountRecords.last();
   }

   public void previous() {
     accountRecords.previous();
   }

   public void next() {
     accountRecords.next();
   }  
}

 


 
sf.dev.ax1103sf.dev.ax1103

Thanks Sidharth.I need pagination on related list not on list view.Above example is for list view.Please give me any example for pagination for related list.

 

 

sf.dev.ax1103sf.dev.ax1103

Hi All,

 

     Any Idea of how to create pagination in custom related list.Here is my page and class. I need to display 5 oppty records.If there are more than 5 records then i need to show a link  Show More link which goes to next page and displays all records like below.

 



 

 

<apex:page standardController="Contact" extensions="sampleDetailPageCon">
<style>
.fewerMore { display: none;}
</style>
<apex:form >
 <apex:pageMessages />
 <apex:detail relatedList="false"></apex:detail>
<apex:pageblock id="CustomList" title="Related Opportunities"  >
   <apex:pageBlockTable value="{!oppz}" var="o" rendered="{!NOT(ISNULL(oppz))}">
        <apex:column value="{!o.Name}"/>
        <apex:column value="{!o.Account.Name}"/>
        <apex:column value="{!o.Type}"/>
       <apex:column value="{!o.Amount}"></apex:column>
       <apex:column value="{!o.CloseDate}"/>
   </apex:pageBlockTable>
   <apex:outputLabel value="No records to display" rendered="{!(ISNULL(oppz))}" styleClass="noRowsHeader"></apex:outputLabel>
 </apex:pageblock>
</apex:form>
</apex:page>

 

 

 

public class sampleDetailPageCon {
    private List<Opportunity> oppz;
    private Contact cntact;
    public sampleDetailPageCon(ApexPages.StandardController controller) {
        this.cntact= (Contact)controller.getRecord();
    }
    public List<Opportunity> getOppz()
    {
        Contact con = [Select id, Account.id FROM Contact where id = :cntact.id];
        if (con.Account == null)
         return null;
        oppz = [Select id, Name, Account.Name, CloseDate, Amount, Type from Opportunity where Account.id = :con.Account.id];
        return oppz;
    }
}

 

Thanks