function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MJRDeveloperMJRDeveloper 

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> 

 

 


And the controller:

public with sharing class myLeadExtension {

My_Lead__c newLead = new My_Lead__c();

String accountnum;

myObj my_obj = new myobj();

public myLeadExtension(ApexPages.StandardController controller) { }

 

public class myObj{

string account = null;

string contact = null;

contact c = new contact();

account a = new account();

 

public contact getcontactobj(){return c;}

public void setcontactobj(contact data){c = data;}

 

public account getaccountobj(){return a;}

public void setaccountobj(account data){a = data;}

 

public string getaccount(){return account;}

public void setaccount(string data){account = data;}

 

public string getcontact(){return contact;}

public void setcontact(string data){contact = data;}

}

 

public myObj getmyObj() {

if (my_obj == null) { my_obj = new myObj(); }

return my_obj;

}

 

public my_lead__c getmylead(){return newLead;}

 

public void setmylead(my_lead__c data){ newlead = data;}

 

public Account getAccount() {

list<account> a = [SELECT name, accountnumber, phone FROM account where id = :newlead.account__c limit 1 ];

if(a.size() != 0) {my_obj.a = a[0];}

return my_obj.a;

}

 

public list<selectoption> getContactName() {

string selectedaccount = my_obj.a.accountnumber;

list<contact> c = [SELECT name FROM contact where Account.accountnumber = :selectedaccount order by lastname];

List<SelectOption> option = new List<SelectOption>();

 

for(integer i=0; i < c.size(); i++) {

option.add(new SelectOption(c[i].name,c[i].name));}

 

return option;

}

 

public contact getselectedcontact(){

string scontact = null;

string sAccount = my_obj.a.accountnumber;

if (my_obj != null){ scontact = my_obj.contact;}

else{ scontact = 'test';}

 

list<contact> c = [SELECT name,phone,firstname,lastname, fax, email FROM contact where Account.accountnumber = :saccount and name = :scontact limit 1];

 

if(c.size() != 0) {my_obj.c = c[0];}

else{return new contact();}

return my_obj.c;

}

 

public String getAcctNum(){ return accountNum; }

public void setAcctNum(String a){ accountNum = a;}

 

public PageReference step1() { return Page.MyLeadsPG1; }

public PageReference step2() { return Page.myLeadspg2; }

public PageReference step3() { return Page.MyLeadsPG3; }

 

public PageReference newcontact() {

my_obj.c = new contact();

return Page.MyLeadsContact; }

 

public PageReference save() {

newlead.account__c = my_obj.a.id;

newlead.contact__c = my_obj.c.id;

insert newLead;

PageReference redirectPage = new PageReference('/' + newLead.ID);

return redirectPage;

}

 

public pagereference savecontact(){

my_obj.c.accountid = my_obj.a.id;

insert my_obj.c;

my_obj.contact = my_obj.c.name;

return page.MyLeadsPG2; }

 

 

 
 
Message Edited by MJRDeveloper on 06-28-2009 02:07 PM
Best Answer chosen by Admin (Salesforce Developers) 
mtbclimbermtbclimber

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

mtbclimbermtbclimber

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; } }

 

 

This was selected as the best answer
MJRDeveloperMJRDeveloper
Thank you so much! this is exactly what I needed. The reason for the hidden inputfield is because I am overiding the new my_lead button with this visualforce page, so I need the hidden inputfield to pull in the account the user was on when they hit the new button. There may have been another way to do what I wanted, but this works beautifully. thank you again.
mtbclimbermtbclimber

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

MJRDeveloperMJRDeveloper
I did see the other question regarding the custom lead object. My company uses the lead object for sales leads, but this project is for leads generated from calls to our customer service reps. I want to distinguish between the two and a simple custom object does the job nicely.
mtbclimbermtbclimber
Cool. Thanks for responding.