Dynamic Dropdown Filtering¶
A project accounting team codes each timesheet to a department and a project. When the indexer picks a Department, the ProjectCode dropdown should list only that department's projects, and a project from another department must not stay selected.
At a glance
- Extension point Custom Index Behavior
- You need
Department(DropDownList:Engineering,Marketing,Finance) andProjectCode(DropDownList); permission to edit content types in the Connections app - Teaches
SetFieldChoicesfrom a map in the script, clearing a value that is no longer in the list
Large or changing lists belong in a source
If the mapping lives in a database table, use the built-in Filter behavior instead. Use this recipe for short, stable lists. For a source-backed list, see Handle a Failed Lookup in a Cascading Dropdown.
Set it up¶
- In the Connections app, open the content type.
- Select
Department. Open Behaviors > Add a Behavior > Custom Behavior. - Name it
Projects by department, paste the script below, and select Save & Close.
ProjectCode can keep every project in its configured choices. The script replaces the list at run time.
Script¶
// Attach to: Department
// Reads: Department, ProjectCode. Writes: choices, visibility and value of ProjectCode.
var projectsByDepartment = {
"Engineering": ["ENG-001", "ENG-002", "ENG-003"],
"Marketing": ["MKT-001", "MKT-002"],
"Finance": ["FIN-001", "FIN-002"]
};
var department = panel["Department"].RuntimeValue || "";
var projects = projectsByDepartment[department] || [];
var current = panel["ProjectCode"].RuntimeValue || "";
builder.SetFieldChoices("ProjectCode", projects);
builder.SetFieldIsVisible("ProjectCode", projects.length > 0);
if (current !== "" && projects.indexOf(current) === -1) {
// true = do not run ProjectCode's own behaviors for this reset
builder.SetFieldRuntimeValue("ProjectCode", "", true);
}
How it works¶
- The map holds each department's projects. The keys must match the
Departmentchoices exactly. SetFieldChoicesreplaces theProjectCodelist. Each string is stored as both display text and value.ProjectCodehides when the department has no projects, or when no department is picked.- A project that is not in the new list is cleared, so a timesheet never keeps a project from another department.
Test it¶
- In the editor, select Test. In the preview panel, select
Marketing.ProjectCodelists onlyMKT-001andMKT-002. - Select
MKT-001, then changeDepartmenttoFinance.ProjectCodeis cleared and lists the finance projects. - Clear
Department.ProjectCodehides.
You are done when every department shows only its projects in the Index app.
What can go wrong¶
Watch out
- The list does not change and the field shows "could not be fired". The script uses
builder.SetChoices, which is broken. Usebuilder.SetFieldChoices. - No project shows for a department. The department text in the map does not match the choice exactly, including case and spaces.
- Stored values change.
SetFieldChoicesstores each string as both display text and value. Use the codes you want stored. - A saved timesheet shows an empty project. The panel load runs the script. A stored project that is not in the department's list is cleared. Keep retired codes in the map while documents still use them.
- A server run clears a project. Run Indexing Behaviors and Start QC Task run the script too. A project a workflow step wrote that is not in the map is cleared there as well.
Variations¶
Three levels (Region, Country, City). Attach one script to Region that sets the Country list, and one to Country that sets the City list. When the Region script clears Country, pass false as the third argument so the Country script runs and resets City.