[SalesForce] Pass a value from Visualforce Page OutputField

I need to use an OutputField on a Visualforce page, to filter a different SOQL query.

I have two unrelated objects – Account and Label…

The idea is that on the page load, the label of the Account Name field is filtered to that of the country that the account resides.

Im not sure how to build the controller to retrieve the country, or whether the field has to pass from the VF page?

One way of doing this would be to pass through the field in the URL. I would rather not do it this way.

I have added the code, leaving in the url passthrough, so as to help anyone else that might want to do it this way. Just concatenate the relevant fields (the url would look something like this – /apex/vfpage?id=a1FN0000000cEaw&country=Australia)

Any help would be appreciated

Thanks

VF:

<apex:page controller="fieldPassController" >

<apex:form >
<apex:repeat value="{!thisAccount}" var="thisAcc">
    <apex:outputField value="{!thisAcc.Country__c}"/>

    <apex:repeat value="{!label}" var="lab">
        <apex:outputField value="{!lab.Name}"/>
    </apex:repeat>

    <apex:inputField value="{!thisAcc.Name}" />
</apex:repeat>
</apex:form>

</apex:page>

Controller:

public class fieldPassController {

public list <account> thisAccount {get;set;}
public list <labels__c> label {get;set;}
public Id thisAccountID = ApexPages.currentPage().getParameters().get('Id');
//public string thisAccountCountry = ApexPages.currentPage().getParameters().get('Country');
//public string thisAccountCountry = thisAccount.Country__c;

    public fieldPassController() {

        thisAccount = [
            SELECT
                Country__c,
                id,
                name

            FROM Account
            WHERE id = :thisAccountID];

        label = [
            SELECT
                Country__c,
                id,
                name

            FROM Labels__c
            //WHERE Country__c = :thisAccountCountry
            ];
    }
}

Best Answer

In your controller, uncomment the WHERE part of the Labels__c SOQL query and change it to:

WHERE Country__c = :thisAccount[0].Country__c

This works because you've already queried for the account and stored the result in thisAccount. Then you don't need to pass the country as a URL param.

Also, if this page is only for one account, you don't actually need to do repeats in the Visualforce page. You could just store the results of the query in a single SObject, though I would still store them in a List initially (see SOQL return type: List<sObject> vs. sObject).