You need to sign in to do that
Don't have an account?
Passing inputfield to controller
Ok, I am stuck. I am working on a wizard to create a custom lead object. I've posted the entire VF page and controller extension. I can't figure out why the getaccount() function is not returning the account specified by the hidden inputfield on the visualforce page. Any help is appreciated.
Here is the VF page:
<apex:page standardcontroller="My_Lead__c" extensions="myLeadExtension">
<apex:sectionHeader title="New Lead Capture" />
<apex:form >
<apex:pageBlock title="Page 1">
<apex:facet name="footer">
<apex:outputPanel id="footerbtn">
<apex:commandButton action="{!step1}" value="Back" styleClass="btn"/>
<apex:commandButton action="{!save}" value="Save" styleClass="btn"/>
</apex:outputPanel>
</apex:facet>
<apex:inputField id="accountNum" value="{!mylead.Account__c}" rendered="false"/>
<apex:pageBlockSection title="Account Information">
<apex:pageblocksectionitem >
<apex:outputlabel for="anum" value="Account Number"/>
<apex:outputfield id="anum" value="{!account.accountnumber}"/>
</apex:pageblocksectionitem>
<apex:pageblocksectionitem >
<apex:outputlabel for="aphone" value="Account Phone"/>
<apex:outputfield id="aphone" value="{!account.phone}"/>
</apex:pageblocksectionitem>
<apex:pageblocksectionitem >
<apex:outputlabel for="aname" value="Account Name"/>
<apex:outputfield id="aname" value="{!account.name}"/>
</apex:pageblocksectionitem>
</apex:pageblocksection>
<apex:pageBlockSection title="Select Contact">
<apex:pageblocksectionitem >
<apex:outputlabel for="cname" value="Contact Name"/>
<apex:selectList id="cname" size="1" value="{!myobj.contact}">
<apex:selectoption itemLabel="-Select One-" itemValue="-Select One-"/>
<apex:selectoptions value="{!contactname}"></apex:selectoptions>
<apex:actionSupport event="onchange" rerender="details"/>
</apex:selectList>
</apex:pageblocksectionitem>
<apex:pageblocksectionitem >
<apex:commandButton action="{!newcontact}" value="New Contact" styleClass="btn"/>
</apex:pageblocksectionitem>
</apex:pageblocksection>
<apex:pageblocksection id="details" title="Contact Details">
<apex:pageblocksectionitem >
<apex:outputlabel for="cphone" value="Contact Phone"/>
<apex:outputfield id="cphone" value="{!selectedcontact.phone}"/>
</apex:pageblocksectionitem>
</apex:pageblocksection>
<apex:pageblockSection title="Lead Information">
<apex:pageblocksectionitem >
<apex:panelGrid columns="4">
<apex:outputlabel value="Product" for="prod" />
<apex:inputtext id="prod" value="{!mylead.myproduct__c}"/>
<apex:outputlabel value="Quantity" for="qty"/>
<apex:inputtext id="qty" value="{!mylead.Quantity__c}"/>
<apex:outputlabel value="In Stock" for="is"/>
<apex:selectList id="is" value="{!mylead.In_Stock__c}" size="1">
<apex:selectoption itemLabel="No" itemValue="No"/>
<apex:selectoption itemLabel="Yes" itemValue="Yes"/>
</apex:selectlist>
<apex:outputlabel value="Sample Requested" for="sample"/>
<apex:selectList id="Sample" value="{!mylead.Sample__c}" size="1">
<apex:selectoption itemLabel="No" itemValue="No"/>
<apex:selectoption itemLabel="Yes" itemValue="Yes"/>
</apex:selectList>
</apex:panelgrid>
</apex:pageblocksectionitem>
</apex:pageblocksection>
<apex:pageblocksection >
<apex:pageblocksectionitem >
<apex:panelGrid columns="2" width="100%">
<apex:outputlabel value="End Use" for="end"/>
<apex:inputTextarea id="end" cols="60" rows="5" value="{!mylead.End_Use__c}"/>
<apex:outputlabel value="Notes" for="notes"/>
<apex:inputTextarea id="nots" cols="90" rows="10" value="{!mylead.notes__c}"/>
</apex:panelgrid>
</apex:pageblocksectionitem>
</apex:pageblockSection>
</apex:pageblock>
</apex:form>
</apex:page>
If the component is not rendered then the user can never supply a value. Is the value being set on some other (preceding) page in your wizard? If not then the value is always going to be null as far as I can tell. If so then I don't see why you even need that component in your page.
It's also a bit confusing as to why you are using an inner class for what appears to be a subset of your custom object. The standardcontroller provides a my_lead__c object for you as well as a save method that does pretty much exactly what you are trying to accomplish in your save method.
I'm also curious as to why the custom object for Lead when there is a standard one already. Are you aware of the standard Lead object? If so, why the custom one?
For reference I refactored your code a bit to a smaller footprint which I think will work just the same as yours.
Note: I also added the pagemessages component to your page so you can see any errors thrown by the standard save method. You should add some exception handling as well though I realize you are still in development and may just not have gotten to it yet.
<apex:page standardcontroller="My_Lead__c" extensions="myleadExtension"> <apex:sectionHeader title="New Lead Capture" /> <apex:form > <apex:pageBlock title="Page 1"> <apex:pageMessages/> <apex:facet name="footer"> <apex:outputPanel id="footerbtn"> <apex:commandButton action="{!step1}" value="Back" styleClass="btn"/> <apex:commandButton action="{!save}" value="Save" styleClass="btn"/> </apex:outputPanel> </apex:facet> <apex:inputField id="accountNum" value="{!My_Lead__c.Account__c}" rendered="false"/> <apex:pageBlockSection title="Account Information"> <apex:pageblocksectionitem > <apex:outputlabel for="anum" value="Account Number"/> <apex:outputfield id="anum" value="{!account.accountnumber}"/> </apex:pageblocksectionitem> <apex:pageblocksectionitem > <apex:outputlabel for="aphone" value="Account Phone"/> <apex:outputfield id="aphone" value="{!account.phone}"/> </apex:pageblocksectionitem> <apex:pageblocksectionitem > <apex:outputlabel for="aname" value="Account Name"/> <apex:outputfield id="aname" value="{!account.name}"/> </apex:pageblocksectionitem> </apex:pageblocksection> <apex:pageBlockSection title="Select Contact"> <apex:pageblocksectionitem > <apex:outputlabel for="cname" value="Contact Name"/> <apex:selectList id="cname" size="1" value="{!My_Lead__c.contact__c}"> <apex:selectoption itemLabel="-Select One-" itemValue="-Select One-"/> <apex:selectoptions value="{!contactname}"/> <apex:actionSupport event="onchange" rerender="details"/> </apex:selectList> </apex:pageblocksectionitem> <apex:pageblocksectionitem > <apex:commandButton action="{!newcontact}" value="New Contact"/> </apex:pageblocksectionitem> </apex:pageblocksection> <apex:pageblocksection id="details" title="Contact Details"> <apex:pageblocksectionitem > <apex:outputlabel for="cphone" value="Contact Phone"/> <apex:outputfield id="cphone" value="{!selectedcontact.phone}"/> </apex:pageblocksectionitem> </apex:pageblocksection> <apex:pageblockSection title="Lead Information"> <apex:pageblocksectionitem > <apex:panelGrid columns="4"> <apex:outputlabel value="Product" for="prod" /> <apex:inputtext id="prod" value="{!My_Lead__c.myproduct__c}"/> <apex:outputlabel value="Quantity" for="qty"/> <apex:inputtext id="qty" value="{!My_Lead__c.Quantity__c}"/> <apex:outputlabel value="In Stock" for="is"/> <apex:selectList id="is" value="{!My_Lead__c.In_Stock__c}" size="1"> <apex:selectoption itemLabel="No" itemValue="No"/> <apex:selectoption itemLabel="Yes" itemValue="Yes"/> </apex:selectlist> <apex:outputlabel value="Sample Requested" for="sample"/> <apex:selectList id="Sample" value="{!My_Lead__c.Sample__c}" size="1"> <apex:selectoption itemLabel="No" itemValue="No"/> <apex:selectoption itemLabel="Yes" itemValue="Yes"/> </apex:selectList> </apex:panelgrid> </apex:pageblocksectionitem> </apex:pageblocksection> <apex:pageblocksection > <apex:pageblocksectionitem > <apex:panelGrid columns="2" width="100%"> <apex:outputlabel value="End Use" for="end"/> <apex:inputTextarea id="end" cols="60" rows="5" value="{!My_Lead__c.End_Use__c}"/> <apex:outputlabel value="Notes" for="notes"/> <apex:inputTextarea id="nots" cols="90" rows="10" value="{!My_Lead__c.notes__c}"/> </apex:panelgrid> </apex:pageblocksectionitem> </apex:pageblockSection> </apex:pageblock> </apex:form> </apex:page>
public with sharing class myLeadExtension { My_Lead__c newLead; public String newContactName { get; set; } public myLeadExtension(ApexPages.StandardController controller) { newLead = (My_Lead__c) controller.getRecord(); } public Account getAccount() { list<account> a = [SELECT name, accountnumber, phone FROM account where id = :newlead.account__c limit 1 ]; if(a.size() != 0) {newlead.account__r = a[0];} return newlead.account__r; } public list<selectoption> getContactName() { List<SelectOption> option = new List<SelectOption>(); for(Contact c:[SELECT name FROM contact where Account.accountnumber = :newlead.account__r.accountnumber order by lastname]) { option.add(new SelectOption(c.name,c.name)); } return option; } public contact getselectedcontact(){ list<contact> c = [SELECT name,phone,firstname,lastname, fax, email FROM contact where Account.accountnumber = :newlead.account__r.accountNumber and name = :newContactName limit 1]; if(c.size() != 0) {newLead.contact__r = c[0];} else{return new contact();} return newLead.contact__r; } public PageReference step1() { return Page.MyLeadsPG1; } public PageReference step2() { return Page.myLeadspg2; } public PageReference step3() { return Page.MyLeadsPG3; } public PageReference newcontact() { newLead.contact__r = new contact(); return Page.MyLeadsContact; } public pagereference savecontact(){ newLead.contact__r.accountid = newLead.account__c; insert newLead.contact__r; return page.MyLeadsPG2; } }
All Answers
If the component is not rendered then the user can never supply a value. Is the value being set on some other (preceding) page in your wizard? If not then the value is always going to be null as far as I can tell. If so then I don't see why you even need that component in your page.
It's also a bit confusing as to why you are using an inner class for what appears to be a subset of your custom object. The standardcontroller provides a my_lead__c object for you as well as a save method that does pretty much exactly what you are trying to accomplish in your save method.
I'm also curious as to why the custom object for Lead when there is a standard one already. Are you aware of the standard Lead object? If so, why the custom one?
For reference I refactored your code a bit to a smaller footprint which I think will work just the same as yours.
Note: I also added the pagemessages component to your page so you can see any errors thrown by the standard save method. You should add some exception handling as well though I realize you are still in development and may just not have gotten to it yet.
<apex:page standardcontroller="My_Lead__c" extensions="myleadExtension"> <apex:sectionHeader title="New Lead Capture" /> <apex:form > <apex:pageBlock title="Page 1"> <apex:pageMessages/> <apex:facet name="footer"> <apex:outputPanel id="footerbtn"> <apex:commandButton action="{!step1}" value="Back" styleClass="btn"/> <apex:commandButton action="{!save}" value="Save" styleClass="btn"/> </apex:outputPanel> </apex:facet> <apex:inputField id="accountNum" value="{!My_Lead__c.Account__c}" rendered="false"/> <apex:pageBlockSection title="Account Information"> <apex:pageblocksectionitem > <apex:outputlabel for="anum" value="Account Number"/> <apex:outputfield id="anum" value="{!account.accountnumber}"/> </apex:pageblocksectionitem> <apex:pageblocksectionitem > <apex:outputlabel for="aphone" value="Account Phone"/> <apex:outputfield id="aphone" value="{!account.phone}"/> </apex:pageblocksectionitem> <apex:pageblocksectionitem > <apex:outputlabel for="aname" value="Account Name"/> <apex:outputfield id="aname" value="{!account.name}"/> </apex:pageblocksectionitem> </apex:pageblocksection> <apex:pageBlockSection title="Select Contact"> <apex:pageblocksectionitem > <apex:outputlabel for="cname" value="Contact Name"/> <apex:selectList id="cname" size="1" value="{!My_Lead__c.contact__c}"> <apex:selectoption itemLabel="-Select One-" itemValue="-Select One-"/> <apex:selectoptions value="{!contactname}"/> <apex:actionSupport event="onchange" rerender="details"/> </apex:selectList> </apex:pageblocksectionitem> <apex:pageblocksectionitem > <apex:commandButton action="{!newcontact}" value="New Contact"/> </apex:pageblocksectionitem> </apex:pageblocksection> <apex:pageblocksection id="details" title="Contact Details"> <apex:pageblocksectionitem > <apex:outputlabel for="cphone" value="Contact Phone"/> <apex:outputfield id="cphone" value="{!selectedcontact.phone}"/> </apex:pageblocksectionitem> </apex:pageblocksection> <apex:pageblockSection title="Lead Information"> <apex:pageblocksectionitem > <apex:panelGrid columns="4"> <apex:outputlabel value="Product" for="prod" /> <apex:inputtext id="prod" value="{!My_Lead__c.myproduct__c}"/> <apex:outputlabel value="Quantity" for="qty"/> <apex:inputtext id="qty" value="{!My_Lead__c.Quantity__c}"/> <apex:outputlabel value="In Stock" for="is"/> <apex:selectList id="is" value="{!My_Lead__c.In_Stock__c}" size="1"> <apex:selectoption itemLabel="No" itemValue="No"/> <apex:selectoption itemLabel="Yes" itemValue="Yes"/> </apex:selectlist> <apex:outputlabel value="Sample Requested" for="sample"/> <apex:selectList id="Sample" value="{!My_Lead__c.Sample__c}" size="1"> <apex:selectoption itemLabel="No" itemValue="No"/> <apex:selectoption itemLabel="Yes" itemValue="Yes"/> </apex:selectList> </apex:panelgrid> </apex:pageblocksectionitem> </apex:pageblocksection> <apex:pageblocksection > <apex:pageblocksectionitem > <apex:panelGrid columns="2" width="100%"> <apex:outputlabel value="End Use" for="end"/> <apex:inputTextarea id="end" cols="60" rows="5" value="{!My_Lead__c.End_Use__c}"/> <apex:outputlabel value="Notes" for="notes"/> <apex:inputTextarea id="nots" cols="90" rows="10" value="{!My_Lead__c.notes__c}"/> </apex:panelgrid> </apex:pageblocksectionitem> </apex:pageblockSection> </apex:pageblock> </apex:form> </apex:page>
public with sharing class myLeadExtension { My_Lead__c newLead; public String newContactName { get; set; } public myLeadExtension(ApexPages.StandardController controller) { newLead = (My_Lead__c) controller.getRecord(); } public Account getAccount() { list<account> a = [SELECT name, accountnumber, phone FROM account where id = :newlead.account__c limit 1 ]; if(a.size() != 0) {newlead.account__r = a[0];} return newlead.account__r; } public list<selectoption> getContactName() { List<SelectOption> option = new List<SelectOption>(); for(Contact c:[SELECT name FROM contact where Account.accountnumber = :newlead.account__r.accountnumber order by lastname]) { option.add(new SelectOption(c.name,c.name)); } return option; } public contact getselectedcontact(){ list<contact> c = [SELECT name,phone,firstname,lastname, fax, email FROM contact where Account.accountnumber = :newlead.account__r.accountNumber and name = :newContactName limit 1]; if(c.size() != 0) {newLead.contact__r = c[0];} else{return new contact();} return newLead.contact__r; } public PageReference step1() { return Page.MyLeadsPG1; } public PageReference step2() { return Page.myLeadspg2; } public PageReference step3() { return Page.MyLeadsPG3; } public PageReference newcontact() { newLead.contact__r = new contact(); return Page.MyLeadsContact; } public pagereference savecontact(){ newLead.contact__r.accountid = newLead.account__c; insert newLead.contact__r; return page.MyLeadsPG2; } }
You don't need the "hidden" inputfield for the puropose you suggest. The use of the record provided by the standardController in your extension will do what you want, i.e. be instantiated with the accountId when coming from the relatedlist on account.
Did you see my other question above about why the custom object vs. the standard Lead object? I'm still curious as others probably are at this point too :smileyhappy:
Thanks,
Andrew