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
StephenYS10000StephenYS10000 

Dynamic picklist connected to custom object

Hi

 

I'm trying to link a custom field from the Account object that will create a picklist based on data from a custom lookup object

It works but I'm having problems getting to selected the current record value and to save the current picklist value to the Account record

 

Thanks in advance for you help

 

 

apex:page standardController="Account" >  <apex:form >  <tr><td>Municapaly</td><td>   <p>test {!account.Municipaly__c}</p>       <c:Municapaly_Dynamic_Picklist defaultvalue="(!account.Municipaly__c"> </c:Municapaly_Dynamic_Picklist>   </td>  <tr>    </apex:form></apex:page>              <apex:component controller="clsMuniciplayDynamicPicklist" ><apex:attribute name="defaultvalue" description="Dynamic Picklist value" type="String" required="true"></apex:attribute>  <apex:selectList value="{!defaultvalue}" size="1"><apex:selectOptions value="{!items}"></apex:selectOptions></apex:selectList></apex:component>  public class clsMuniciplayDynamicPicklist{    private Account CurrentAccount;        public string defaultvalue{get;set;}          private List<SelectOption> items;        public List<SelectOption> getItems() {         List<SelectOption> items = new List<SelectOption>();          for (Lookup_Tables__c lt: [Select l.Type__c, l.Start_Date__c, l.Name, l.End_Date__c, l.Code__c From Lookup_Tables__c l]){           items.add(new SelectOption( lt.Code__c,lt.Name));                 } 
StephenYS10000StephenYS10000

apex:page standardController="Account" > <apex:form > <tr><td>Municapaly</td><td> <p>test {!account.Municipaly__c}</p> <c:Municapaly_Dynamic_Picklist defaultvalue="(!account.Municipaly__c"> </c:Municapaly_Dynamic_Picklist> </td> <tr> </apex:form> </apex:page> <apex:component controller="clsMuniciplayDynamicPicklist" > <apex:attribute name="defaultvalue" description="Dynamic Picklist value" type="String" required="true"></apex:attribute> <apex:selectList value="{!defaultvalue}" size="1"> <apex:selectOptions value="{!items}"> </apex:selectOptions> </apex:selectList></apex:component> public class clsMuniciplayDynamicPicklist { private Account CurrentAccount; public string defaultvalue{get;set;} private List<SelectOption> items; public List<SelectOption> getItems() { List<SelectOption> items = new List<SelectOption>(); for (Lookup_Tables__c lt: [Select l.Type__c, l.Start_Date__c, l.Name, l.End_Date__c, l.Code__c From Lookup_Tables__c l]){ items.add(new SelectOption( lt.Code__c,lt.Name)); }

 

bob_buzzardbob_buzzard

Your defaultvalue attribute in your controller isn't associated with any attribute value, as you aren't using assignTo.  However, you do have a property called defaultvalue in the controller, which may well be confusing matters.

 

 

StephenYS10000StephenYS10000

Hi Bob

 

Thanks for your tip regarding the attribute value.

 

One more question. How would I bind the selected picklist to the underlying object field?

 

 

Thanks

 

Stephen 

 

bob_buzzardbob_buzzard

I think by the time your field is in the component, it is disconnected from the account object.  I seem to recall that the field will be immutable if you pass it as an attribute, so you either need it in a wrapper class or in the original sobject.  You'd probably need to pass the account object into the component like so:

 

 

apex:page standardController="Account" > <apex:form > <tr><td>Municapaly</td><td> <p>test {!account.Municipaly__c}</p> <c:Municapaly_Dynamic_Picklist defaultvalue="(!account"> </c:Municapaly_Dynamic_Picklist> </td> <tr> </apex:form> </apex:page> <apex:component controller="clsMuniciplayDynamicPicklist" > <apex:attribute name="defaultvalue" description="Account containing Dynamic Picklist value" type="Account" required="true"></apex:attribute> <apex:selectList value="{!account.Municipaly__c}" size="1"> <apex:selectOptions value="{!items}"> </apex:selectOptions> </apex:selectList> </apex:component> public class clsMuniciplayDynamicPicklist { private List<SelectOption> items; public List<SelectOption> getItems() { List<SelectOption> items = new List<SelectOption>(); for (Lookup_Tables__c lt: [Select l.Type__c, l.Start_Date__c, l.Name, l.End_Date__c, l.Code__c From Lookup_Tables__c l]){ items.add(new SelectOption( lt.Code__c,lt.Name)); }

 

 I've taken the account and defaultvalue properties out of your class, as I don't think you'll need them any more.  However, I haven't tested this so you may find they need to go back in.

 

StephenYS10000StephenYS10000

Thanks for the prompt feedback

 

I will test it now

StephenYS10000StephenYS10000

Hi Bob

 

I'm getting errors when I try and compile the new controller

Unknown property 'clsMuniciplayDynamicPicklist.account' Create Apex property 'clsMuniciplayDynamicPicklist.account' Create Apex method 'clsMuniciplayDynamicPicklist.getAccount'

 

Unknown property 'clsMuniciplayDynamicPicklist.account'
Quick FixCreate Apex property 'clsMuniciplayDynamicPicklist.account'
Quick FixCreate Apex method 'clsMuniciplayDynamicPicklist.getAccount'

bob_buzzardbob_buzzard

Typo on my part.  Change the attribute name in the component to account.

 

e.g.

 

 

<apex:attribute name="account" description="Account containing Dynamic Picklist value" type="Account" required="true"></apex:attribute>

 

 

 

StephenYS10000StephenYS10000

 Hi Bob

 

I have implemented the code as you suggested but having problems with the display of the inline Visualforce component in the main Account page. Could you please advise me on how to set is up correctly

<apex:page standardController="Account" showHeader="false" > <apex:form > <apex:outputPanel > <p>test {!account.Municipaly__c}</p> <p><c:Municapaly_Dynamic_Picklist account="{account}"></c:Municapaly_Dynamic_Picklist></p> </apex:outputPanel> </apex:form> </apex:page>

 

<apex:component controller="clsMuniciplayDynamicPicklist" > <apex:attribute name="account" description="Account containing Dynamic Picklist value" type="Account" required="true"></apex:attribute> <apex:selectList value="{!account.Municipaly__c}" size="1"> <apex:selectOptions value="{!items}"> </apex:selectOptions> </apex:selectList> <apex:outputText >test</apex:outputText></apex:component>

 

public class clsMuniciplayDynamicPicklist { public List<SelectOption> getItems() { List<SelectOption> items = new List<SelectOption>(); for (Lookup_Tables__c lt: [Select l.Type__c, l.Start_Date__c, l.Name, l.End_Date__c, l.Code__c From Lookup_Tables__c l]){ items.add(new SelectOption( lt.Code__c,lt.Name)); } return items; } }

 

Thanks in advance

 

Stephen

 

bob_buzzardbob_buzzard
It looks okay to me.  What problems are you getting?
StephenYS10000StephenYS10000

It's displaying the whole Account page where the inline VF page is placed

bob_buzzardbob_buzzard

What page are you trying to embed the VF page into?