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
Suresh440Suresh440 

Checkbox in the Pagenation is not working

Hi All

       i developed a vf page with list of sobject records using pagination now i have kept the checkbox for every record to operate some action on it . Now the problem is when i checked any one of the checkbox and click the next button i am geeting tis error.

System.VisualforceException: Modified rows exist in the records collection!

Error is in expression '{!Next}' in page Listofsalesorders
Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi,

 

Can you provide your code so that it is easy to understand the problem?

Suresh440Suresh440

Thanks for your reply ,here is the code i want to remove the selected record from the list 

 



<apex:page controller="PagingController">
<apex:form >
<apex:pageBlock title="Paging through Categories of Stuff">

<apex:pageBlockButtons location="top">
<apex:commandButton action="{!process1}" value="Process Selected"/>
<apex:commandButton action="{!cancel}" value="Cancel"/>
</apex:pageBlockButtons>
<apex:pageMessages />

<apex:pageBlockSection title="Category Results - Page #{!pageNumber}" columns="1">
<apex:pageBlockTable value="{!categories}" var="c">
<apex:column width="25px">
<apex:inputCheckbox value="{!c.checked}"/>
</apex:column>
<apex:column value="{!c.cat.Name}" headerValue="Name"/>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>

<apex:panelGrid columns="4">
<apex:commandLink action="{!first}">First</apex:commandlink>
<apex:commandLink action="{!previous}" rendered="{!hasPrevious}">Previous</apex:commandlink>
<apex:commandLink action="{!next}">Next</apex:commandlink>
<apex:commandLink action="{!last}">Last</apex:commandlink>
</apex:panelGrid>
<apex:pageBlock >
<apex:pageBlockSection columns="1">
<apex:repeat value="{!acc}" var="line">
{!Line.Name}
</apex:repeat>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

 

----------------------------------------------Apex Class---------------------------------------------------

 

public with sharing class PagingController {
Set<Integer> i = new Set<Integer>();
List<categoryWrapper> categories {get;set;}
public List<string> ls {get;set;}
public List<account> acc{get;set;}
// instantiate the StandardSetController from a query locator
public ApexPages.StandardSetController con {
get {
if(con == null) {
con = new ApexPages.StandardSetController(Database.getQueryLocator([Select Id, Name FROM Account WHERE Check__c=false Order By Name limit 100]));
// sets the number of records in each page set
con.setPageSize(5);
}
return con;
}
set;
}

// returns a list of wrapper objects for the sObjects in the current page set
public List<categoryWrapper> getCategories() {
ls = new List<string>();
categories = new List<categoryWrapper>();
for (Account category : (List<Account>)con.getRecords()){
categories.add(new CategoryWrapper(category));
}
return categories;
}

// displays the selected items
public PageReference process() {
for (CategoryWrapper cw : categories) {
if (cw.checked)
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,cw.cat.name));
}
return null;
}

public void process1()
{

i = new Set<Integer>();
integer j=0;
for(CategoryWrapper c : Categories){
if(c.checked==true)
i.Add(j);
j++;
}
for(Integer k : i ){

categories[k].cat.Check__c=true;
update categories[k].cat;
Categories.remove(k);
}

}

// indicates whether there are more records after the current page set.
public Boolean hasNext {
get {

// return con.getHasNext();
return true;

}
set;
}

// indicates whether there are more records before the current page set.
public Boolean hasPrevious {
get {
return con.getHasPrevious();
}
set;
}

// returns the page number of the current page set
public Integer pageNumber {
get {
return con.getPageNumber();
}
set;
}

// returns the first page of records
public void first() {
con.first();
}

// returns the last page of records
public void last() {
con.last();
}

// returns the previous page of records
public void previous() {
con.previous();
}

// returns the next page of records
public void next() {
con.next();
}

// returns the PageReference of the original page, if known, or the home page.
public void cancel() {
con.cancel();
}

public class CategoryWrapper {

public Boolean checked{ get; set; }
public Account cat { get; set;}

public CategoryWrapper(){
cat = new Account();
checked = false;
}

public CategoryWrapper(Account c){
cat = c;
checked = false;
}



}


}

Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi Suresh,

 

I have found what the problem is!!

 

The problem is, In your controller you are querying the Account details with the condition Check =: false. But, the pageblocktable is displaying  all the records including Check =: true or Check =: false from account. That is why you are getting this error.

 

Check the following highlighted lines in controller,

public with sharing class PagingController {

 .......

public ApexPages.StandardSetController con {
get {
if(con == null) {
con = new ApexPages.StandardSetController(Database.getQueryLocator([Select Id, Name FROM Account WHERE Check__c=false Order By Name limit 100]));        //I tried this controller with removing the where condition i didn't get any error
// sets the number of records in each page set
con.setPageSize(5);
}
return con;
}
set;
}

 

 // returns a list of wrapper objects for the sObjects in the current page set
    public List<categoryWrapper> getCategories() {
        ls = new List<string>();
        categories = new List<categoryWrapper>();
        for (Account category : (List<Account>)con.getRecords()){    //This line gets list of records in 'con', so the table shows all values
            categories.add(new CategoryWrapper(category));
        }
        return categories;
    }

............

}

 

Hope this will help you...!

 

Please don't forget to give kudos by clicking on the Star icon and mark this as a solution, if this works out.