I have a Visualforce page form to create Leads in Salesforce
In the form I want to add the State Picklist.
On the Lead object Address is a composite field. As per documentation, the State field is as seen here -
Field Name | Field Label | Type | Length |
---|---|---|---|
State | State/Province | String | 80 |
I want the State on the visualforce page to look like this. i.e when I click on Select a State , I should see the list of State that i can choose from.
This is the CSS code I tried
<label for="state">State</label>
<select name="state" id="state">
<option value="" selected="selected">Select a State</option>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
<option value="CA">California</option>
<option value="CO">Colorado</option>
<option value="CT">Connecticut</option>
<option value="DE">Delaware</option>
<option value="DC">District Of Columbia</option>
<option value="FL">Florida</option>
<option value="GA">Georgia</option>
<option value="HI">Hawaii</option>
<option value="ID">Idaho</option>
<option value="IL">Illinois</option>
<option value="IN">Indiana</option>
<option value="IA">Iowa</option>
<option value="KS">Kansas</option>
<option value="KY">Kentucky</option>
<option value="LA">Louisiana</option>
<option value="ME">Maine</option>
<option value="MD">Maryland</option>
<option value="MA">Massachusetts</option>
<option value="MI">Michigan</option>
<option value="MN">Minnesota</option>
<option value="MS">Mississippi</option>
<option value="MO">Missouri</option>
<option value="MT">Montana</option>
<option value="NE">Nebraska</option>
<option value="NV">Nevada</option>
<option value="NH">New Hampshire</option>
<option value="NJ">New Jersey</option>
<option value="NM">New Mexico</option>
<option value="NY">New York</option>
<option value="NC">North Carolina</option>
<option value="ND">North Dakota</option>
<option value="OH">Ohio</option>
<option value="OK">Oklahoma</option>
<option value="OR">Oregon</option>
<option value="PA">Pennsylvania</option>
<option value="RI">Rhode Island</option>
<option value="SC">South Carolina</option>
<option value="SD">South Dakota</option>
<option value="TN">Tennessee</option>
<option value="TX">Texas</option>
<option value="UT">Utah</option>
<option value="VT">Vermont</option>
<option value="VA">Virginia</option>
<option value="WA">Washington</option>
<option value="WV">West Virginia</option>
<option value="WI">Wisconsin</option>
<option value="WY">Wyoming</option>
</select> <br>
Can someone please guide, how I could translate this to VFP to use the following tag
<apex:selectOptions value="{!State}" /
No no, the value user selected ({!Lead.State}
) should be bound to parent tag, not to <apex:selectOptions>
. SelectOptions is just list of choices. And then you render them as a picklist (normal or multiselect), list of checkboxes, list of radio buttons... How you use them is separate from how they're defined, which values (if any are disabled...)
If you want to keep it in pure Visualforce - look into <apex:selectOption>
, you could run some search-replace on your code and you're almost there. If you want to generate the list in Apex (if you need it for something else, maybe validations? Maybe you'd want to store it in config object or custom metadata and then admin can tweak the lists without code changes?) - <apex:selectOptions>
and you pass to it a List<SelectOption>
.
If your implementation is only US-specific then maybe you should look into config solution rather than code. https://help.salesforce.com/articleView?id=sf.admin_state_country_picklists_configure.htm&type=5