0

Scenario: I've created a VF page that uses the Account standard controller with an extension that I created to pull contacts related to that account into a multi-selectable list using a wrapper class. Guests that access this page with the correct account ?id= in the URL are able to select multiple existing contacts at a time and, using 2 command buttons, perform a DML update on the selected records to set a Boolean to True or False. This works fine.


Objective: I would like to extend it so that guests that access the page can also add new contacts to the list from the same page, using a separate pageblock and set of command buttons.


I figured I would accomplish this by just making a few public {get; set;} Strings in the extension and adding inputFields to the page that set those public Strings and then make a custom save command that puts together a new contact record related to the account with the values of the strings and does a DML and then a PageReference to reload the page.

This is fine I'm sure for First Name, Last Name, and Email... but we also have a custom Picklist on the Contact object called Title__c. I've never rendered a picklist in VF before, and especially not a picklist from the account's child object through custom extension.

Question: How do I render the Contact picklist, with the correct values, on the custom Account VF page and then get the value that was selected to insert into the new record before DML, so that I can have a 'create new' style form on the page that allows them to dump in new contacts on the fly?

Alternative Question: Can I, alternatively, somehow create my own custom picklist with my own custom values via the extension class itself, and then get the option that they selected and pass it using set; to a string in the extension?

I hope this makes sense, happy to clarify or provide existing extension/page code if deemed necessary.

  • how do you cast the value into the object? – jaw999 Aug 22 '17 at 18:07
1

Controller:

    public List<SelectOption> myPicklist { get;private set; }

    Schema.DescribeFieldResult statusFieldDescription = Contact.FIELD_API_NAME.getDescribe();
    myPicklist = new list<SelectOption>();
    //this line is optional if you want kind of default or empty value
    //myPicklist.add(new SelectOption('--None--','--None--'));
    for (Schema.Picklistentry picklistEntry : statusFieldDescription.getPicklistValues())
    {
        myPicklist.add(new SelectOption(pickListEntry.getValue(),pickListEntry.getLabel()));
    }

VisualForce Page:

<apex:selectList size="1" value="{!VARIABLE_TO_STORE}">
<apex:selectOptions value="{!myPicklist}"/> 
</apex:selectList>

you can add action support onchange to Visualforce if you want to store variable at time it changes.

  • Controller code wasn't perfect, but provided the detail I needed for understanding how to gather picklist values from an existing Salesforce field AND included my alternative question of how to build your own values into a custom controller based picklist. Working great now. +1, Thank you! – Morgan Marchese Jun 15 '16 at 20:26
  • can you share some of the syntax changes you needed to make? thanks! – jaw999 Aug 21 '17 at 20:53
  • how do you cast the value into the object? – jaw999 Aug 22 '17 at 17:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.