0

I'm having 2 visualforce pages with the same class

<apex:pageBlockButtons location="bottom" >  
   <apex:CommandButton value="Search" action="{!Search}" rerender="theblock,themsg" />
   <apex:commandButton value="Cancel" action="{!Cancel}" rerender="thepg" onclick="this.form.reset()"/>  
</apex:pageBlockButtons>

Search function in controller

public void search(){
   // for 1st page query should be
   case c = [select id,name from case where status="new"];
   // for 2nd page query should be without any condition
   case c = [select id,name from case ];
}

Can I get this by putting some condition in the class?

0

Of course, just use

ApexPages.currentPage();

Source

And test its value (thanks to the getUrl()) in the controller method search() to make sure to run the query for the relevant page.

Be careful though not to use the same controller on multiple places if it occurs often. It can have some side effects. The controller will have a lot of complexity just because of being used in multiple places instead of one.

Divide to conquer

1

Pass some value with <apex:param through <apex:commandButton from VF page to Controller. The attribute assignTo is A setter method that assigns the value of this param to a variable in the associated Visualforce controller.

In controller check the value and apply logic for that page.

VF Page

.......
.......
    <apex:commandButton value="Search" action="{!Search}" rerender="theblock,themsg" />
         <apex:param name="eventId" value="fromPage1" assignTo="{!paramValue}"/>
    </apex:commandButton>
.......
.......

Controller

............
............
public String paramValue {get;set;}

............
............
public void search(){
    if(paramValue.equals('fromPage1')){
       // for 1st page query should be
       case c = [select id,name from case where status="new"];  
    }else if(paramValue.equals('fromPage2')){
       // for 2nd page query should be without any condition
       case c = [select id,name from case ];
    }else{
        //To-Do
    }
}
................
................

Working Example

VF Page1

<apex:page controller="SearchController" >
    <apex:form>
        <apex:outputPanel id="theblock">{!callFrom}</apex:outputPanel>
        <apex:commandButton action="{!Search}" value="Search" reRender="theblock"  >
        <apex:param value="fromPage1" assignTo="{!paramValue}" />
        </apex:commandButton>
    </apex:form>
</apex:page>

VF Page 2

<apex:page controller="SearchController" >
    <apex:form>
        <apex:outputPanel id="theblock">{!callFrom}</apex:outputPanel>
        <apex:commandButton action="{!Search}" value="Search" reRender="theblock"  >
        <apex:param value="fromPage2" assignTo="{!paramValue}" />
        </apex:commandButton>
    </apex:form>
</apex:page>

Controller

public class SearchController {
    public String paramValue {get;set;}
    public String callFrom {get;set;}
    public void search(){
        if(paramValue.equals('fromPage1')){
            callFrom = 'Page One';  
        }else if(paramValue.equals('fromPage2')){
            callFrom = 'Page Two'; 
        }else{
            //To-Do
        }
    }    
}
  • how should I get the pagename for the paramvalue if(paramValue.equals('fromPage1')) – Eagerin Sf Mar 8 '17 at 15:51
  • The assignTo attribute automatically pass the parameter to the member variable defined in the Controller. Please find my updated answer with a working example. – Saroj Bera Mar 8 '17 at 16:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.