You need to sign in to do that
Don't have an account?

Pagination with numbering
/** Controller to add Contacts to Portfolio Suspect **/
/** vf page - MultiselectContactAC **/
/** GSI Project **/
/** Owner - Shikha Mishra **/
public with sharing class AddContacttoAttributedCampaign{
public AttributedCampaign__c attc;
public ContactAttributedCampaign__c cac {get;set;}
public list<ContactAttributedCampaign__c> cacList{get;set;}
public string attid;
public string accid;
@TestVisible public list<wrapContact> wrapContactList {get; set;}
public list<Contact> selectedContacts {get; set;}
public integer totalRecs = 0;
public integer index =0;
public integer blockSize = 100;
public integer NoOfRecords = 1;
public integer TotalPages;
public integer sizeOfStartIndex;
public AddContacttoAttributedCampaign(ApexPages.StandardController stdController)
{
this.attc = (AttributedCampaign__c)stdController.getRecord();
attid =ApexPages.currentPage().getParameters().get('attid');
accid =ApexPages.currentPage().getParameters().get('accid');
cac = new ContactAttributedCampaign__c();
attc = new AttributedCampaign__c ();
for(AggregateResult result :[SELECT COUNT(Id) intVal FROM Contact where AccountId=:accid])
{
totalRecs+=(Integer)result.get('intVal');
}
TotalPages = totalRecs/blocksize;
cacList = [Select Name, Attributed_Campaign__c, Contact__c, Include_in_Marketing_Activity__c from ContactAttributedCampaign__c where Attributed_Campaign__c =: attid order by Name];
system.debug('cacList##'+cacList.size()) ;
wrapContactList = new list<wrapContact>();
for (contact c : [Select Id, Name, title, Job_Role__c, Job_Function__c from Contact where AccountId =: accid and Contact_Status__c = 'Active' Order by Name ASC LIMIT : blockSize OFFSET:index ])
{
wrapContactList.add(new wrapContact(c));
}
sizeOfStartIndex = wrapContactList.size();
}
public void beginning()
{
wrapContactList.clear();
NoOfRecords =1;
index = 0;
for (contact c : [Select Id, Name, title, Job_Role__c, Job_Function__c from Contact where AccountId =: accid and Contact_Status__c = 'Active' Order by Name ASC LIMIT : blockSize OFFSET:index])
{
wrapContactList.add(new wrapContact(c));
}
}
public void previous()
{
wrapContactList.clear();
if(NoOfRecords > 1)
{
index = index - blocksize;
for (contact c : [Select Id, Name, title, Job_Role__c, Job_Function__c from Contact where AccountId =: accid and Contact_Status__c = 'Active' Order by Name ASC LIMIT : blockSize OFFSET:index])
{
wrapContactList.add(new wrapContact(c));
}
NoOfRecords=NoOfRecords -1 ;
}
else
{
beginning();
}
}
public void next()
{
wrapContactList.clear();
if(NoOfRecords < TotalPages)
{
index = index + blocksize;
system.debug('index value in last next--' + index);
for (contact c : [Select Id, Name, title, Job_Role__c, Job_Function__c from Contact where AccountId =: accid and Contact_Status__c = 'Active' Order by Name ASC LIMIT : blockSize OFFSET:index])
{
wrapContactList.add(new wrapContact(c));
}
// index = index + wrapContactList.size();
NoOfRecords= NoOfRecords+1;
}
else
{
for (contact c : [Select Id, Name, title, Job_Role__c, Job_Function__c from Contact where AccountId =: accid and Contact_Status__c = 'Active' Order by Name ASC LIMIT : blockSize OFFSET:index])
{
wrapContactList.add(new wrapContact(c));
}
}
}
public void end()
{
wrapContactList.clear();
if(NoOfRecords <=totalPages)
{index = totalRecs - blocksize;
for(contact c : [Select Id, Name, title, Job_Role__c, Job_Function__c from Contact where AccountId =: accid and Contact_Status__c = 'Active' Order by Name ASC LIMIT : blockSize OFFSET:index])
{ wrapContactList.add(new wrapContact(c));} NoOfRecords = TotalPages;
}
}
public boolean getDisableNext()
{
if(sizeOfStartIndex < blocksize)
{
return true;
}
else
{
return false;
}
}
}
Public PageReference SavePage()
{
selectedContacts = new list<Contact>();
Set<Id> cacID = new Set<Id>();
for(ContactAttributedCampaign__c ca :cacList)
{
cacID.add(ca.Contact__c);
}List<ContactAttributedCampaign__c> insertList = new List<ContactAttributedCampaign__c>();
for(wrapContact wrapobj : wrapContactList){
if(wrapobj.isSelected == true)
{
if(!cacID.contains(wrapobj.conn.Id)){
ContactAttributedCampaign__c conac = new ContactAttributedCampaign__c(Attributed_Campaign__c=attid,Contact__c=wrapobj.conn.id,Include_in_Marketing_Activity__c=wrapobj.mark) ;
//conac.Attributed_Campaign__c = attid;
//conac.Contact__c = wrapobj.conn.id;
//conac.Include_in_Marketing_Activity__c= wrapobj.mark;
insertList.add(conac);
// wrapobj.isSelected =False;
}
for(ContactAttributedCampaign__c item : cacList)
{if(item.Contact__c == wrapobj.conn.id && item.Include_in_Marketing_Activity__c != wrapobj.mark)
{ContactAttributedCampaign__c conac = [select id,Include_in_Marketing_Activity__c from ContactAttributedCampaign__c where Attributed_Campaign__c = :attid and Contact__c = :wrapobj.conn.id]; conac.Include_in_Marketing_Activity__c = wrapobj.mark; insertList.add(conac);
}
}
}
}
upsert insertList;
cacList = [Select Name, Attributed_Campaign__c, Contact__c, Include_in_Marketing_Activity__c from ContactAttributedCampaign__c where Attributed_Campaign__c =: attid order by Name desc];
return null;
}
public PageReference redirectBack(){
return new PageReference('/'+attid);
}
@TestVisible public class wrapContact{
public Contact conn{get; set;}
public ContactAttributedCampaign__c cac{get;set;}
public boolean isSelected {get; set;}
public Boolean mark{get;set;}
public wrapContact(Contact c){
this.conn = c;
this.isSelected = false;
}
public wrapContact(ContactAttributedCampaign__c d){
this.cac = d;
this.isSelected = false;
this.mark = d.Include_in_Marketing_Activity__c;
}
}
}
VF Page -
<apex:page standardController="AttributedCampaign__c" extensions="AddContacttoAttributedCampaign" showHeader="false" sidebar="false">
<apex:form id="frm">
<apex:pageMessages />
<apex:pageBlock title="Added Contacts">
<apex:pageBlockTable value="{!cacList}" var="cac">
<apex:column headerValue="Name" value="{!cac.Name}"/>
<apex:column headerValue="Portfolio Suspect" value="{!cac.Attributed_Campaign__c}"/>
<apex:column headerValue="Contact" value="{!cac.Contact__c}"/>
<apex:column headerValue="Include in Marketing Activity" value="{!cac.Include_in_Marketing_Activity__c}"/>
</apex:pageBlockTable>
</apex:pageBlock>
<apex:pageBlock title="Add Contact to Portfolio Suspect" id="pb">
<apex:pageMessages />
<apex:pageBlock id="pb2">
<apex:pageBlockSection columns="1">
<apex:outputPanel id="myPanel" layout="">
<apex:pageBlockTable value="{!wrapContactList}" var="aconl">
<apex:column >
<apex:facet name="header">
</apex:facet>
<apex:inputCheckbox value="{!aconl.isSelected}" id="inputID"/>
</apex:column>
<apex:column headerValue="Name">
<apex:outputfield value="{!aconl.conn.name}"/>
</apex:column>
<apex:column headerValue="Title">
<apex:outputfield value="{!aconl.conn.Title}"/>
</apex:column>
<apex:column headerValue="Job Function">
<apex:outputfield value="{!aconl.conn.Job_Function__c}"/>
</apex:column>
<apex:column headerValue="Job Role">
<apex:outputfield value="{!aconl.conn.Job_Role__c}"/>
</apex:column>
<apex:column headerValue="Include in Marketing Activity">
<apex:inputCheckbox value="{!aconl.mark}" />
</apex:column>
</apex:pageBlockTable>
</apex:outputPanel>
</apex:pageBlockSection>
</apex:pageBlock>
<apex:pageBlockButtons >
<apex:commandButton id="addContact" value="Add" action="{!SavePage}" style="align:center;"/>
<apex:commandButton value="First Page" action="{!beginning}" disabled="{!DisableNext}" reRender="myPanel" />
<apex:commandButton value="Next" action="{!next}" disabled="{!DisableNext}" reRender="myPanel" />
<apex:commandButton value="Prev" action="{!previous}" disabled="{!DisableNext}" reRender="myPanel"/>
<apex:commandButton value="Last Page" action="{!end}" disabled="{!DisableNext}" reRender="myPanel"/>
<apex:commandButton id="redirectBack" value="Done" action="{!redirectBack}"/>
<apex:commandLink action="{!getPageNumbers}" value="P1" rerender="pb"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
Output -

/** vf page - MultiselectContactAC **/
/** GSI Project **/
/** Owner - Shikha Mishra **/
public with sharing class AddContacttoAttributedCampaign{
public AttributedCampaign__c attc;
public ContactAttributedCampaign__c cac {get;set;}
public list<ContactAttributedCampaign__c> cacList{get;set;}
public string attid;
public string accid;
@TestVisible public list<wrapContact> wrapContactList {get; set;}
public list<Contact> selectedContacts {get; set;}
public integer totalRecs = 0;
public integer index =0;
public integer blockSize = 100;
public integer NoOfRecords = 1;
public integer TotalPages;
public integer sizeOfStartIndex;
public AddContacttoAttributedCampaign(ApexPages.StandardController stdController)
{
this.attc = (AttributedCampaign__c)stdController.getRecord();
attid =ApexPages.currentPage().getParameters().get('attid');
accid =ApexPages.currentPage().getParameters().get('accid');
cac = new ContactAttributedCampaign__c();
attc = new AttributedCampaign__c ();
for(AggregateResult result :[SELECT COUNT(Id) intVal FROM Contact where AccountId=:accid])
{
totalRecs+=(Integer)result.get('intVal');
}
TotalPages = totalRecs/blocksize;
cacList = [Select Name, Attributed_Campaign__c, Contact__c, Include_in_Marketing_Activity__c from ContactAttributedCampaign__c where Attributed_Campaign__c =: attid order by Name];
system.debug('cacList##'+cacList.size()) ;
wrapContactList = new list<wrapContact>();
for (contact c : [Select Id, Name, title, Job_Role__c, Job_Function__c from Contact where AccountId =: accid and Contact_Status__c = 'Active' Order by Name ASC LIMIT : blockSize OFFSET:index ])
{
wrapContactList.add(new wrapContact(c));
}
sizeOfStartIndex = wrapContactList.size();
}
public void beginning()
{
wrapContactList.clear();
NoOfRecords =1;
index = 0;
for (contact c : [Select Id, Name, title, Job_Role__c, Job_Function__c from Contact where AccountId =: accid and Contact_Status__c = 'Active' Order by Name ASC LIMIT : blockSize OFFSET:index])
{
wrapContactList.add(new wrapContact(c));
}
}
public void previous()
{
wrapContactList.clear();
if(NoOfRecords > 1)
{
index = index - blocksize;
for (contact c : [Select Id, Name, title, Job_Role__c, Job_Function__c from Contact where AccountId =: accid and Contact_Status__c = 'Active' Order by Name ASC LIMIT : blockSize OFFSET:index])
{
wrapContactList.add(new wrapContact(c));
}
NoOfRecords=NoOfRecords -1 ;
}
else
{
beginning();
}
}
public void next()
{
wrapContactList.clear();
if(NoOfRecords < TotalPages)
{
index = index + blocksize;
system.debug('index value in last next--' + index);
for (contact c : [Select Id, Name, title, Job_Role__c, Job_Function__c from Contact where AccountId =: accid and Contact_Status__c = 'Active' Order by Name ASC LIMIT : blockSize OFFSET:index])
{
wrapContactList.add(new wrapContact(c));
}
// index = index + wrapContactList.size();
NoOfRecords= NoOfRecords+1;
}
else
{
for (contact c : [Select Id, Name, title, Job_Role__c, Job_Function__c from Contact where AccountId =: accid and Contact_Status__c = 'Active' Order by Name ASC LIMIT : blockSize OFFSET:index])
{
wrapContactList.add(new wrapContact(c));
}
}
}
public void end()
{
wrapContactList.clear();
if(NoOfRecords <=totalPages)
{index = totalRecs - blocksize;
for(contact c : [Select Id, Name, title, Job_Role__c, Job_Function__c from Contact where AccountId =: accid and Contact_Status__c = 'Active' Order by Name ASC LIMIT : blockSize OFFSET:index])
{ wrapContactList.add(new wrapContact(c));} NoOfRecords = TotalPages;
}
}
public boolean getDisableNext()
{
if(sizeOfStartIndex < blocksize)
{
return true;
}
else
{
return false;
}
}
}
Public PageReference SavePage()
{
selectedContacts = new list<Contact>();
Set<Id> cacID = new Set<Id>();
for(ContactAttributedCampaign__c ca :cacList)
{
cacID.add(ca.Contact__c);
}List<ContactAttributedCampaign__c> insertList = new List<ContactAttributedCampaign__c>();
for(wrapContact wrapobj : wrapContactList){
if(wrapobj.isSelected == true)
{
if(!cacID.contains(wrapobj.conn.Id)){
ContactAttributedCampaign__c conac = new ContactAttributedCampaign__c(Attributed_Campaign__c=attid,Contact__c=wrapobj.conn.id,Include_in_Marketing_Activity__c=wrapobj.mark) ;
//conac.Attributed_Campaign__c = attid;
//conac.Contact__c = wrapobj.conn.id;
//conac.Include_in_Marketing_Activity__c= wrapobj.mark;
insertList.add(conac);
// wrapobj.isSelected =False;
}
for(ContactAttributedCampaign__c item : cacList)
{if(item.Contact__c == wrapobj.conn.id && item.Include_in_Marketing_Activity__c != wrapobj.mark)
{ContactAttributedCampaign__c conac = [select id,Include_in_Marketing_Activity__c from ContactAttributedCampaign__c where Attributed_Campaign__c = :attid and Contact__c = :wrapobj.conn.id]; conac.Include_in_Marketing_Activity__c = wrapobj.mark; insertList.add(conac);
}
}
}
}
upsert insertList;
cacList = [Select Name, Attributed_Campaign__c, Contact__c, Include_in_Marketing_Activity__c from ContactAttributedCampaign__c where Attributed_Campaign__c =: attid order by Name desc];
return null;
}
public PageReference redirectBack(){
return new PageReference('/'+attid);
}
@TestVisible public class wrapContact{
public Contact conn{get; set;}
public ContactAttributedCampaign__c cac{get;set;}
public boolean isSelected {get; set;}
public Boolean mark{get;set;}
public wrapContact(Contact c){
this.conn = c;
this.isSelected = false;
}
public wrapContact(ContactAttributedCampaign__c d){
this.cac = d;
this.isSelected = false;
this.mark = d.Include_in_Marketing_Activity__c;
}
}
}
VF Page -
<apex:page standardController="AttributedCampaign__c" extensions="AddContacttoAttributedCampaign" showHeader="false" sidebar="false">
<apex:form id="frm">
<apex:pageMessages />
<apex:pageBlock title="Added Contacts">
<apex:pageBlockTable value="{!cacList}" var="cac">
<apex:column headerValue="Name" value="{!cac.Name}"/>
<apex:column headerValue="Portfolio Suspect" value="{!cac.Attributed_Campaign__c}"/>
<apex:column headerValue="Contact" value="{!cac.Contact__c}"/>
<apex:column headerValue="Include in Marketing Activity" value="{!cac.Include_in_Marketing_Activity__c}"/>
</apex:pageBlockTable>
</apex:pageBlock>
<apex:pageBlock title="Add Contact to Portfolio Suspect" id="pb">
<apex:pageMessages />
<apex:pageBlock id="pb2">
<apex:pageBlockSection columns="1">
<apex:outputPanel id="myPanel" layout="">
<apex:pageBlockTable value="{!wrapContactList}" var="aconl">
<apex:column >
<apex:facet name="header">
</apex:facet>
<apex:inputCheckbox value="{!aconl.isSelected}" id="inputID"/>
</apex:column>
<apex:column headerValue="Name">
<apex:outputfield value="{!aconl.conn.name}"/>
</apex:column>
<apex:column headerValue="Title">
<apex:outputfield value="{!aconl.conn.Title}"/>
</apex:column>
<apex:column headerValue="Job Function">
<apex:outputfield value="{!aconl.conn.Job_Function__c}"/>
</apex:column>
<apex:column headerValue="Job Role">
<apex:outputfield value="{!aconl.conn.Job_Role__c}"/>
</apex:column>
<apex:column headerValue="Include in Marketing Activity">
<apex:inputCheckbox value="{!aconl.mark}" />
</apex:column>
</apex:pageBlockTable>
</apex:outputPanel>
</apex:pageBlockSection>
</apex:pageBlock>
<apex:pageBlockButtons >
<apex:commandButton id="addContact" value="Add" action="{!SavePage}" style="align:center;"/>
<apex:commandButton value="First Page" action="{!beginning}" disabled="{!DisableNext}" reRender="myPanel" />
<apex:commandButton value="Next" action="{!next}" disabled="{!DisableNext}" reRender="myPanel" />
<apex:commandButton value="Prev" action="{!previous}" disabled="{!DisableNext}" reRender="myPanel"/>
<apex:commandButton value="Last Page" action="{!end}" disabled="{!DisableNext}" reRender="myPanel"/>
<apex:commandButton id="redirectBack" value="Done" action="{!redirectBack}"/>
<apex:commandLink action="{!getPageNumbers}" value="P1" rerender="pb"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
Output -
Thanks
Surinder
A good sample of code:
http://www.sfdcpoint.com/salesforce/pagination-using-standard-set-controller-salesforce/
You should have used a StandardSetController which has the methods getResultSize() (returns the number of records in the set), getPageSize() (returns the number of records included in each page set) and getPageNumber()
(returns the page number of the current page set. Note that the first page returns 1) as a standard feature.
The total pages is easily calculated from getResultSize() and getPageSize().
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_pages_standardsetcontroller.htm
Regards
Alain
Thanks for the information you shared!
But this link can not be used in my case as record count is around 1600. And we cant display more than 1000 records in if we go in this way.
Regards,
Surinder
The maximum record limit for StandardSetController is 10,000 records.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_pages_standardsetcontroller.htm
There could be a problem with your code (compared with a StandardSetController) :
for(contact c : [Select Id, Name, title, Job_Role__c, Job_Function__c from Contact where AccountId =: accid and Contact_Status__c = 'Active' Order by Name ASC LIMIT : blockSize OFFSET:index])
The maximum offset is 2,000 rows. Requesting an offset greater than 2,000 results in a NUMBER_OUTSIDE_VALID_RANGE error.
https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_offset.htm
Best regards
Alain