+ Start a Discussion
Steven HoughtalenSteven Houghtalen 

How to use inputfield with a custom controller?

Greetings,  I hope someone can help me with this. I am developing a custom controller which uses inputfield in the visualpage.  Where  I am stumped is how to capture that data in a variable.  The visualforce page and the controller are listed below.  I have stripped much of the lines from each to only focus on the issue I am having.  Meds__c is a custom object which contain a lookup field Staff__c.  When I execute this code, I get a null pointer exception on the "staff = meds.staff__c;" line.  Thank you.

<apex:page Controller="DispMeds"  sidebar="false">
    <apex:pageBlock title="Test - DispMeds "/>
   <apex:form >
     <apex:pageBlock >
      <apex:inputfield value="{!meds.Staff__c}"/>

public with sharing class DispMeds {
    Public Id Client {get;set;}
    public  Meds__c  meds {get;set;}
    Public string staff;
    public DispMeds(){
        system.debug('Start of Controller');
        ID ClientID = ApexPages.currentPage().getParameters().get('id');
        staff = meds.staff__c;
        system.debug('Staff = ....' + staff);

Alain CabonAlain Cabon

Do you have: meds = new Meds__c();  somewhere in your code ?
Steven HoughtalenSteven Houghtalen
No I don't.  Is that all I need or is there more ?
Alain CabonAlain Cabon
Here is the template that you need: 


account = (id == null) ? new Account() : [SELECT Name, Phone, Industry FROM Account WHERE Id = :id];

public class NewAndExistingController {

    public Account account { get; private set; }

    public NewAndExistingController() {
        Id id = ApexPages.currentPage().getParameters().get('id');
        account = (id == null) ? new Account() : 
            [SELECT Name, Phone, Industry FROM Account WHERE Id = :id];

    public PageReference save() {
        try {
        } catch(System.DMLException e) {
            return null;
        //  After successful Save, navigate to the default view page
        PageReference redirectSuccess = new ApexPages.StandardController(Account).view();
        return (redirectSuccess);

<apex:page controller="NewAndExistingController" tabstyle="Account">
        <apex:pageBlock mode="edit">
                <apex:inputField value="{!Account.name}"/>
                <apex:inputField value="{!Account.phone}"/>
                <apex:inputField value="{!Account.industry}"/>
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="Save" action="{!save}"/>


Steven HoughtalenSteven Houghtalen
Thanks for your response Alain.  But, I don't understand it. You get a field input from the VP and pass it to the contoller.  Your controller does a query of a record and then saves it.  How does the Industry, Phone, and Name get updated in the record before saving ot updating the record?

As an aside, I am not capturing the variable to update the same record.  I am using the variable to create a record in a different object.
Alain CabonAlain Cabon
It is a ternary operator or the syntax for a basic conditional expression used in several programming languages (not only Apex)  https://en.wikipedia.org/wiki/%3F: 

account = (id == null) ? new Account() : [SELECT Name, Phone, Industry FROM Account WHERE Id = :id];

is equivalent to:

if (id == null) {
      account = new Account();
} else {
     account =  [SELECT Name, Phone, Industry FROM Account WHERE Id = :id];

You can trust Salesforce for this pattern of code. That works.


Steven HoughtalenSteven Houghtalen
Hi Alain,  thanks for the explantion of "= (id == null) ?  but that is not my problem.  Let me to try to explain a different way by using a sequence of the code I originally posted.
1) Launch the visualforce page
2) Select a staff member from the staff lookup field on the form
3) Move the results from #2 into the variable "staff"

I can't figure out how to do #3 despite reading every bit of documentation on the use of inputfield.  They all stop short of how to capture the results and put it in a variable.

Thanks for your patience.
Alain CabonAlain Cabon

Where is the button "save" and its associated code? You have used a custom controller.
<apex:pageBlockButtons location="bottom"> <apex:commandButton value="Save" action="{!save}"/> </apex:pageBlockButtons>

Replace the object and the fields in the template code above (NewAndExistingController) with your object and your fields and post the result (other people could help you if there is at least a basic understandable structure, your code seems a joke).

In a standard controller, you define just the position of the standard save button in your VF page.

<apex:page standardController="Account" ...

Without the word "standard" before "Controller", you are using a custom controller.

Steven HoughtalenSteven Houghtalen
Alain,  what I posted was some prototype code extracted from 75 lines of code ( a best practice) that work...including the code for "save".  Yes, it is a custom controller because it needs to be. The question posed was pretty simple; how to pass the data from a VP inputfield to a string varaible in the controller.  I have concluded you don't know to do this and regrettably, rather tnan say so, chose to be critical.