1

Hi all i am trying to access the inputfield value that the user selects and then pass it to an SOQL query in my controller extension then return the SOQL result to an outputText compunent, my code is shown below:

Visualforce

<apex:page standardController="Ships_Book__c" extensions="sBookController">
    <apex:pageBlock title="Ships Book">
        <apex:pageBlockSection >
            <apex:form >
                <apex:inputField value="{!Ships_Book__c.Vessel_Name__c}" id="Vessel_Name">
                    <apex:actionSupport event="onchange" action="{!setsWeight}"/>
                </apex:inputField>
                Vessel Weight&nbsp; &nbsp; &nbsp;
                   <apex:outputText value="{0}">
                        <apex:param value="{!sWeight}"/>
                    </apex:outputText>
            </apex:form>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:page>

Apex

public class sBookController {

    private ApexPages.StandardController stdCtrl {get; Set;}

    public integer sWeight;

    public sBookController(ApexPages.StandardController std) {
        stdCtrl = std;        
    }

    public integer getsWeight() {
        return sWeight;
    }

    public void setsWeight() {

        sWeight= [SELECT Vessel_weight__C FROM Vessel_Weight__c WHERE Vessel_Name__c =: ship.Vessel_Name__c Limit 1];
    }
}

Any help would be greatly appreciated as i am just learning

Thanks

5
  • sWeight is declared as an integer and you are assigning the result of query i.e. List<Vessel_Weight__c>.
    – Shilpa
    Commented Sep 15, 2017 at 10:41
  • Can you be more specific about the problems? I assume this code doesn't compile. Commented Sep 15, 2017 at 11:45
  • I am trying to get the selected option from the inputlist and pass this to the query in the controller extension but i don't know how to reference it in the query? Thanks
    – aCoder75
    Commented Sep 15, 2017 at 11:55
  • It's worth mentioning that you should take additional care in this situation. Accepting user input and using it in a query makes you vulnerable to SOQL injection attacks. It's more of a concern with dynamic SOQL, but my experience in administrating websites has pretty much hard-wired me to say user input always requires sanitization (when it's used in a query). I'd suggest reading the documentation on SOQL Injection.
    – Derek F
    Commented Sep 15, 2017 at 13:09
  • 1
    @DerekF : what is the purpose of binding then ? He is using static SOQL. Please read the documentation and his code before sharing information. Link : developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/… Commented Sep 15, 2017 at 13:25

1 Answer 1

1

You need to get the standard record associated with the ApexPages.StandardController and then use that record in the SOQL.

public class sBookController {

    private Ships_Book__c ship {get; set;}
    public integer sWeight;

    public sBookController(ApexPages.StandardController std) {        
        ship = (Ships_Book__c)stdCtrl.getRecord();
    }

    public integer getsWeight() {
        return sWeight;
    }

    public void setsWeight() {      
        List<Vessel_Weight__c> lstVesselWeight = [SELECT Vessel_weight__C FROM Vessel_Weight__c WHERE Vessel_Name__c =: ship.Vessel_Name__c Limit 1];
        if(!lstVesselWeight,isEmpty())
            sWeight = lstVesselWeight[0].Vessel_weight__C;
    }
}

Note:

  1. I removed the ApexPages.StandardController and created a object for Ships_Book__c
  2. Instead of assigning the query to the integer directly, i assigned it to the list, check for not empty and assign the value to integer.

Hope it helps.

3
  • How can i pass the 1stVesselWeight back to the visualforce page to display it, Thanks for your help with this
    – aCoder75
    Commented Sep 15, 2017 at 13:14
  • The setter won't ever get called this way?
    – Adrian Larson
    Commented Sep 15, 2017 at 13:42
  • @AdrianLarson setsWeight method is used as an action method here not as a setter for the sWeight variable. With that understanding only i have provided the solution. Commented Sep 15, 2017 at 14:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .