You need to sign in to do that
Don't have an account?
Steven 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}"/>
</apex:pageBlock>
</apex:form
</apex:page>
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);
}
}
<apex:page Controller="DispMeds" sidebar="false">
<apex:pageBlock title="Test - DispMeds "/>
<apex:form >
<apex:pageBlock >
<apex:inputfield value="{!meds.Staff__c}"/>
</apex:pageBlock>
</apex:form
</apex:page>
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);
}
}
Do you have: meds = new Meds__c(); somewhere in your code ?
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_custom.htm
account = (id == null) ? new Account() : [SELECT Name, Phone, Industry FROM Account WHERE Id = :id];
Controller:
VF:
Regards
Alain
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.
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.
Regards
Alain
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.
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.
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/apex_pages_standardcontroller.htm
<apex:page standardController="Account" ...
Without the word "standard" before "Controller", you are using a custom controller.
Regards
Alain