+ Start a Discussion
StephenYS10000StephenYS10000 

Dynamic picklist

Does anybody know how I can populate a picklist from a trigger

 

My client whats the picklist to be dynamic

Best Answer chosen by Admin (Salesforce Developers) 
JavajJavaj

 

Working Code for you: 

 

Here i am displaying the list of accounts as a picklist (that dynamically collect the data from the Account object)  on contact.

 

So link your  'MunicipalityList__c' object to account. use this below code will work.

 

<apex:page standardcontroller="Contact" Extensions="ChildExtension">

<apex:actionStatus id="status">

 <apex:facet name="start">

   <c:loading BackColor="#efefef" borderColor="#336699"
borderSize="3" height="50px" width="120px"
ImageUrl="{!$Resource.AjaxAnimation}"  Message="Loading..."
messageStyle="color:darkred;font-size:11pt;font-weight:bold;"/>

 </apex:facet>

</apex:actionStatus>



<apex:form >

<apex:pageBlock mode="edit" id="thePageBlock">

<apex:pageBlockButtons >

<apex:commandButton value="Save" action="{!save}"/>

<apex:commandButton value="Cancel" action="{!cancel}"/>

</apex:pageBlockButtons>

<apex:pageblockSection id="ParentList" title="Dynamic Picklist">

<apex:pageBlockSectionItem >

<apex:outputLabel value="Dynamic Pick List field" for="p"/>

<apex:selectList id="p" value="{!Contact.name}" size="1" title="3">

    <apex:selectOptions value="{!PositionOptions}"/>

</apex:selectList>

</apex:pageBlockSectionItem>

</apex:pageblocksection>

</apex:pageBlock>

</apex:form>

</apex:page>
 

 

 

public class ChildExtension {
private final Contact child;
    public ChildExtension(ApexPages.StandardController controller) {
        this.child = (Contact)controller.getRecord();
        }
    public List
<selectOption> PositionOptions {get
        {
        List
<selectOption> parents = new List
<selectOption>();
        for (Account prt : [select name from Account pt])
            parents.add(new selectOption(prt.id, prt.name));
            return parents;
        }
    private set;
    }
}
 

 

All Answers

bob_buzzardbob_buzzard

Can you explain a little more about what populating a picklist means?

 

E.g. are you trying to populate a picklist field in an sObject from a trigger, change the available values in the picklist etc

 

A little more about how you want to use it would be useful too - why it needs to be done in a trigger, for example. 

StephenYS10000StephenYS10000

Hi

 

We have a custom object called 'MunicipalityList__c' that is populated from an external source using the Castiron sync software

 

The custom object will for example contain a new list of Municipalites for this companies list of customers

 

I want to create a compontent and VF page that I can add to the Account layout page. The Account object will have a custom field called Municipality__c. I want to conpontent to act as a picklist that dynamically collect the data from the 'MunicipalityList__c' custom object.

 

Thanks in advance

 

Stephen

 

JavajJavaj

 

Working Code for you: 

 

Here i am displaying the list of accounts as a picklist (that dynamically collect the data from the Account object)  on contact.

 

So link your  'MunicipalityList__c' object to account. use this below code will work.

 

<apex:page standardcontroller="Contact" Extensions="ChildExtension">

<apex:actionStatus id="status">

 <apex:facet name="start">

   <c:loading BackColor="#efefef" borderColor="#336699"
borderSize="3" height="50px" width="120px"
ImageUrl="{!$Resource.AjaxAnimation}"  Message="Loading..."
messageStyle="color:darkred;font-size:11pt;font-weight:bold;"/>

 </apex:facet>

</apex:actionStatus>



<apex:form >

<apex:pageBlock mode="edit" id="thePageBlock">

<apex:pageBlockButtons >

<apex:commandButton value="Save" action="{!save}"/>

<apex:commandButton value="Cancel" action="{!cancel}"/>

</apex:pageBlockButtons>

<apex:pageblockSection id="ParentList" title="Dynamic Picklist">

<apex:pageBlockSectionItem >

<apex:outputLabel value="Dynamic Pick List field" for="p"/>

<apex:selectList id="p" value="{!Contact.name}" size="1" title="3">

    <apex:selectOptions value="{!PositionOptions}"/>

</apex:selectList>

</apex:pageBlockSectionItem>

</apex:pageblocksection>

</apex:pageBlock>

</apex:form>

</apex:page>
 

 

 

public class ChildExtension {
private final Contact child;
    public ChildExtension(ApexPages.StandardController controller) {
        this.child = (Contact)controller.getRecord();
        }
    public List
<selectOption> PositionOptions {get
        {
        List
<selectOption> parents = new List
<selectOption>();
        for (Account prt : [select name from Account pt])
            parents.add(new selectOption(prt.id, prt.name));
            return parents;
        }
    private set;
    }
}
 

 

This was selected as the best answer