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
ToddKruseToddKruse 

Trying to prevent a page redirect when clicking a search button on a VF page.

I have a VF page that I want to use to overwrite the standard page the user sees when they click the "Projects" tab.  Currently, clicking the tab brings you to the layout that we defined in house, and all is pretty.

 

However, when I overwrite the "Standard Buttons and Links" "Tab" option with my code that I will show below, it loads fine to begin with.  Then when the user selects a project from a selectList dropdown and clicks the "Search" button, they are pulled away from my new custom page and brought right back to the original "Projects" page.

 

Here is my VF code:

 

 

<apex:page controller="VFProjectClass" sidebar="false" showHeader="true">

 <apex:pageBlock title="My Projects">  

  <apex:form > 

   

   <apex:pageBlockSection id="meetSelectionSection2" >

 <apex:selectList id="selectId" value="{!selectId}" multiselect="false"  size="1">

     <apex:selectOptions value="{!rectypes}"/>

 </apex:selectList>  

 

  <apex:commandButton action="{!search2}" value="Search" id="searchBtn" >

 

  </apex:commandButton>

 

    <apex:outputPanel id="OP2" layout="block">

    <div style="width:1000px">

   <apex:detail />

   </div>

   </apex:outputPanel>

   </apex:pageBlockSection>

  </apex:form> 

 </apex:pageBlock>

</apex:page> 

 

And here is the Class code dealing with the "search2" code of the commandButton:

 

public String selectId

{

get;

set;

}

   

public PageReference search2() 

{

Id newValue = selectId;

 

SFDC_Projects__c acc = [Select Id From SFDC_Projects__c Where id =: newValue Limit 1];

 

PageReference next = new PageReference('/'+ acc.id);

   next.setRedirect(true);

    return next;

 

 

}

 

Any help would be great on this.  I want to prevent moving away from this page.  I just want the lower section of code, the "<apex:detail />" to refresh with the information that was selected in the dropdown list.

 

Thanks

 

--Todd Kruse 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
jwetzlerjwetzler

If you return null, you'll stay on the same page.  On your button specify rerender="OP2" so that it rerenders that bottom portion of your page.

 

Finally, you need to give your detail component a subject attribute, which is the id of the record it should display. So inside of search2() you should be storing off some id value that you want to show the detail for, and then bind your subject to that id.

All Answers

jwetzlerjwetzler
You have a search button bound to the {!search2} action.  In your search2() method you are creating a pageReference, setting it to redirect, and returning it.  And you're saying you don't want the page to redirect?  If that's what you're returning from your action method, that's what the button is going to do.
ToddKruseToddKruse

Thanks for the reply.  Yeah, I can see the point to your logic and it makes sense.  How would I then prevent the entire page to redirect and just have the "<apex:detail />" section of the code "refresh"?

 

Thanks again

 

--Todd Kruse 

jwetzlerjwetzler

If you return null, you'll stay on the same page.  On your button specify rerender="OP2" so that it rerenders that bottom portion of your page.

 

Finally, you need to give your detail component a subject attribute, which is the id of the record it should display. So inside of search2() you should be storing off some id value that you want to show the detail for, and then bind your subject to that id.

This was selected as the best answer
ToddKruseToddKruse

Jill, you are a true genius!  That was exactly what I needed to do and the page looks perfect now.

 

Thanks for your quick response and expert advice.

 

--Todd Kruse 

ToddKruseToddKruse

I have a follow up question.  I have the code in place, and its displaying the information perfectly.  When the user clicks the "Search" button, the lower portion of the page refreshes with the correct information.  However, when they click the "edit" button, they are redirected away from my page and when they click "Save" on that form, they are brought back to my new page but not back to the project they were viewing.  

 

I looked at the URL for a page that was using a standard page and when an edit button was clicked, it looked like this:

 

https://xxxxx.salesforce.com/a03T00000013Jxz/e?retURL=/a03T00000013Jxz 

 

but my custom page looks like this:

 

https://xxxxx.salesforce.com/a03T00000013Jxz/e?retURL=https://c.cs0.visual.force.com/apex/testProjectPage 

 

How do I have it direct itself back to what they viewed before the edit button was clicked?

 

Thanks

 

--Todd Kruse