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
lescclarklescclark 

Passing & Using variables

Hi

 

I am new apex and have developed my first code this week.

 

I have created VF pages with extended controllers quite simply to run basic queries & to return results.

 

My 2 issues are:

 

i) I navigate to a VF page that utilises an extended controller, lets say accounts.  When I land on the page I want to assign a value to a variable, let's say assign value of account.city__c to myVariable.  The page then displays a list of Contacts where contact.city__c = myVariable. I pretty much have this working but I can't assign & use myVariable automatically when I navigate to the page, I am having to initiate the method through a commandLink in the VF page which uses assignto to assign the variable & display the list.

 

ii) this is probably a similar and related issue - I have a vf page that uses inputtext to capture some search text.  When hitting a commandbutton, navigate to another page that displays the results.  Likewise, I have this working in principal but don't know how to pass the list of results to the new page.

 

I'm pretty sure that these are due to my lack of knowledge of variables & have tried to get my head round them via various sources but just can't seem to find a solution.

 

Thanksin advance

 

Les

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

The syntax you are looking for is:

 

 

PageReference acctPage = Page.newpage; acctPage.setRedirect(true); acctPage.getParameters().put('city', acct.City); return acctPage;

 

 Then you can extract this in your controller like so:

 

 

String city=ApexPages.currentPage().getParameters().get('city');

 

 If you want the datatable to be populated when the page is displayed, do away with the doCourseSearch and change getCourseResults to retrieve the data as follows:

 

 

List<Course__c> CourseResults; public List<Course__c> getCourseResults() { if (null==CourseResults) { CourseResults = [select Name, Contact__r.Salutation, id, Contact__r.FirstName from course__c where category2__c = :acct.name]; } return CourseResults; }

 

That way when the getter runs when building the page, the list will be populated if it hasn't been already.

 

 

 

 

All Answers

bob_buzzardbob_buzzard

You can set the value of the variable in the constructor of an extension controller.  The constructor takes a reference to the standard controller, from which you can retrieve the account record.  Then simply assign the city to a property on the controller.  In your method that builds the list of Contacts, simply reference the value that you assigned earlier.  Or you could capture a reference to the account record in the constructor and use that, or capture a reference to the standard controller and dereference that when constructing the list.

 

With regard to a search page, I wouldn't advocate passing the list of results to the page as this would be quite restrictive in terms of the data that you could pass (as they go on the URL).  Rather I would pass the search string to the new page (via a property on the PageReference that generates the redirect) and execute the search in the new page controller as part of a method that displays the results.  In fact, I'd suggest for this that you display the results in the same page, using the VF re-rendering capability.

 

 

lescclarklescclark

Re issue i) I have now partly managed to get this working simply using

 

[select Name, Contact__r.Salutation, id, Contact__r.FirstName from course__c where category2__c = :acct.name];

 

- must have been my fat fingers. 

 

 

However, I still can't get the list to display automatically when I land on the page.

 

haven't solved ii) yet either !!

bob_buzzardbob_buzzard

If you can post some code/VF snippets, we can probably provide some targetted help.  I've using the behaviour you desire in a number of places, so it can be done!

 

 

lescclarklescclark

Hi

 

thanks for the help.  I hadn't thought of rerendering the page, will give it a go.  However, lets say I want to pass 'searchString' into contact.city__c via a url to apex/newPage what is the syntax ?????????????  to ensure that I pick up the correct field.

 

PageReference acctPage = Page.newpage ?????????????????;
    acctPage.setRedirect(true);
    return acctPage;

 

 

Back to the first question:

 

this is the data table that I want to show automatically when landing on the page.  As mentioned, I have now managed to pick up the variable acct.name.  I guess {!CourseResults} is 'empty' as {!doCourseSearch} doesn't automatically run.

 

 

 

 

<apex:dataTable value="{!CourseResults}" id="courseListBlock" var="item" width="100%" styleClass="datatablestyle" columnClasses="colclass" >                   

                 <apex:column headerValue="Course name" >
                    <apex:outputField value="{!item.name}"/>
                    </apex:column>                      
                    <apex:column headerValue="Id" >
                    <apex:outputField value="{!item.id}"></apex:outputField>                   
                    </apex:column>
      </apex:dataTable>

 

 

 

 

 

here is  the controlller

 

 

 thanks

 

public class Draft2AllObjectExtension {
private final Category__c acct;
public Draft2AllObjectExtension(ApexPages.StandardController stdController) {
this.acct = (Category__c)stdController.getRecord();
}

List<Course__c> CourseResults;
public List<Course__c> getCourseResults() {
return CourseResults;
}
public PageReference doCourseSearch() {
CourseResults = (List<Course__c>)[select Name, Contact__r.Salutation, id, Contact__r.FirstName from course__c where category2__c = :acct.name];
return null;
}
bob_buzzardbob_buzzard

The syntax you are looking for is:

 

 

PageReference acctPage = Page.newpage; acctPage.setRedirect(true); acctPage.getParameters().put('city', acct.City); return acctPage;

 

 Then you can extract this in your controller like so:

 

 

String city=ApexPages.currentPage().getParameters().get('city');

 

 If you want the datatable to be populated when the page is displayed, do away with the doCourseSearch and change getCourseResults to retrieve the data as follows:

 

 

List<Course__c> CourseResults; public List<Course__c> getCourseResults() { if (null==CourseResults) { CourseResults = [select Name, Contact__r.Salutation, id, Contact__r.FirstName from course__c where category2__c = :acct.name]; } return CourseResults; }

 

That way when the getter runs when building the page, the list will be populated if it hasn't been already.

 

 

 

 

This was selected as the best answer
neckrneckr

I have two extension controllers for my standard account controller.  I declared a variable in my one extension controllers which is used to filter query results for both controllers. Is there a way to pass the value of the variable from one controller to the next?