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
sangigupsangigup 

StandardSetController save method

Hello Gurus,

 

I have a VisualForce page that is calling a Custom StandardSetController with Pagination. I go to page #2 and update a record. After I hit save, I am directed back to first page although {!CurrentPage} displays Page #2. How can I stay on the current record/Page after I hit save?

Here is the relevant code:

 

Controller code:

public with sharing class SMADummy {
    
public ApexPages.StandardSetController setCon {
    get {
        if (setCon== null){
            system.debug('Calling locator');
        setCon=new ApexPages.StandardSetController(Database.getQueryLocator(
                            [Select s.Name, Activity_Start_Date__c,Activity_End_Date__c From Dummy__c s order by name limit 30]));
                 
                
               
            }
            return setCon;
        }
        set;
    }
   public List<Smart_Marketing_Activity__c> getActivities() {
     
         return (List<Smart_Marketing_Activity__c>) setCon.getRecords();
 }

 

VF Code:

<apex:page controller="SMADummy">

<apex:form>

<apex:pageBlock>

    <apex:pageBlockButtons >
            
                <apex:CommandButton value="Save" action="{!save}" />
              
              </apex:pageBlockButtons>
          

  <apex:commandLink action="{!setCon.previous}" immediate="true" >Previous</apex:commandlink>
    <apex:commandLink action="{!setCon.next}" immediate="true">Next</apex:commandlink>
     <apex:pageBlockTable value="{!Activities}" var="SMA" >

 

 <apex:column >
               <apex:facet name="header">Activity Start Date</apex:facet>
               <apex:inputField value="{!SMA.Activity_Start_Date__c}" />
            </apex:column>

....

....

 </apex:pageBlockTable>
      
     </apex:pageBlock>
  </apex:form>

</apex:page>

aballardaballard

You probably need to override save in your controller with a method that calls the standard controller save, then returns null to stay on the same page. 

 

sangigupsangigup

Thanks for your reply. I tried the following but it did not make any difference.

 

 

 

public PageReference save() {
  
        try{
             setCon.save();
         } catch (Exception e){
             ApexPages.Message myMsg = new ApexPages.Message(ApexPages.severity.ERROR, 'No records matched the search criteria ' + e.getMessage());
            ApexPages.addMessage(myMsg);
            System.debug('Error is======'+e.getMessage());
             
         }        
         
       return null;
    }

 

 

sangigupsangigup

I noticed that although I am sent back to the first page after save, the page number getPageNumber does not change. It still shows value = 2 if I am on the second page and hit save. One way to stay on page #2 after save was to do next() in a loop until I hit the PageNumber=2.

Patrick DixonPatrick Dixon

Try adding:-

 

a new method

 

    public void SaveSet() {
        setCon.save();
   }

 

and call {!SaveSet} instead of {!save} in the VF page.  If you declare it as PageReference rather than void, you can return any page you like.

 

(If that helps you, could you share the rest of your controller code as I'm having trouble passing the updated records back to the standardSetController in my code? :-(  ... see:- http://boards.developerforce.com/t5/Apex-Code-Development/How-do-I-save-with-a-StandardSetController-with-extensions/td-p/287521 )

Sanchivan SivadasanSanchivan Sivadasan

I had the same issue and I tried to put next() in a loop but it did not work for me. I finally played around with it and found that it works if I do the following:

 

public void save() {
	con.save();
	if(pageNumber != 1) {
		con.setPageNumber(pageNumber - 1);
		con.next();
	}
}

 Hope this will help someone else in the future.