PeopleCode | Creating a specific list of values for a dropdown field.
A. Dropdown Lists for Validation
The dropdown field presents you with a full list of valid values upon clicking the down-arrow next to the field. The values are always sorted in alphabetical order:
A dropdown field is normally associated with an SQL table or view. Whenever the user accesses the field, the dropdown will display all records currently in the table or view. However, the list of values remains static at any one time. You cannot pick-and-choose the values you wish to see. In some cases, this might do the job fine, but in other cases, you’d prefer to restrict the dropdown to a specific list of values.
This requirement often comes up for dropdowns that need to work dynamically. That is, the list of values has to change depending on user security or other data already entered on the page. A common scenario is that entry of Field 2 is dependent on what the user has already entered for Field 1. So if Field 1 is ‘Department’ and Field 2 is ‘Employee’, you will want the Field 2 dropdown to only display employees that belong to the selected department in Field 1. You do not want the user selecting any employee in the entire company.
B. Creating a Custom Dropdown List
This custom dropdown list requirement can be achieved via the ‘ClearDropDownList’ and ‘AddDropDownItem’ methods. Both of these work in relation to field objects.
‘ClearDropDownList’ does not require any parameters. It simply clears all existing values from the dropdown list.
‘AddDropDownItem’ requires two parameters: the field code (eg ‘GBR’), and the field description (‘Great Britain’). The first parameter is used for saving in the database record; the second parameter is what the user sees displayed on the page.
The combination of these two methods allows you to specify an exact list of values for a dropdown list. Firstly, here’s an example where the dropdown values are hardcoded:
/* Remove all existing dropdown values */ XX_REC_NAME.COUNTRY.ClearDropDownList(); /* Add dropdown values */ XX_REC_NAME.COUNTRY.AddDropDownItem ("AUS", "Australia"); XX_REC_NAME.COUNTRY.AddDropDownItem ("NZL", "New Zealand"); XX_REC_NAME.COUNTRY.AddDropDownItem ("ZAF", "South Africa");
Here’s another example, this time using a rowset object. This restricts the country drop-down to all values where the name begins with ‘B’:
/* Remove all existing dropdown values */ XX_REC_NAME.COUNTRY.ClearDropDownList(); &CountryRS = CreateRowset(Record.COUNTRY_TBL); &country_count = &CountryRS.Fill("where DESCR like 'B%'"); If &country_count > 0 Then For &i = 1 To &country_count &CountryRec = &CountryRS.GetRow(&i).COUNTRY_TBL; XX_REC_NAME.COUNTRY.AddDropDownItem(&CountryRec.COUNTRY.Value, &CountryRec.DESCR.Value); End-For; End-If;
See Also: