Handle a Failed Lookup in a Cascading Dropdown¶
A project accounting team picks a Department, and a Filter behavior loads that department's projects into ProjectCode from a SQL table. When the source is down, or the department has no rows, the indexer sees "Could not retrieve data from source." and an empty dropdown, or worse, the previous department's projects. This recipe makes the failure visible and gives the indexer a way to keep working.
At a glance
- Extension point Custom Index Behavior, with a built-in Filter behavior
- You need
Department(DropDownList) with a Filter behavior that fillsProjectCode;ProjectCode(DropDownList, no static choices);ProjectCodeManual(Textbox); permission to edit content types in the Connections app - Teaches Behavior order as a pipeline (reset, look up, check), reading
Choicesafter a Filter, a fallback field
Why two scripts¶
When the Filter's query fails, it shows its message on Department and leaves the ProjectCode list as it was. A script that runs after the Filter cannot tell an old list from a new one. So the first script empties the list before the Filter runs. After the Filter, an empty list means "nothing came back", whatever the cause.
graph TD
A["User picks a Department"] --> B["1. Reset: empty the ProjectCode list"]
B --> C["2. Filter: load projects from the source"]
C --> D["3. Check: is the list still empty?"]
D --> E["Yes: show ProjectCodeManual with a message"]
D --> F["No: hide and clear ProjectCodeManual"]
Set it up¶
- In the Connections app, open the content type.
- Select
ProjectCodeand remove its static choices. The Filter fills the list. - Select
Department. Add a Custom Behavior namedReset project listwith Script 1. - On
Department, check the Filter behavior: its Destination Column isProjectCode. - On
Department, add a second Custom Behavior namedCheck project listwith Script 2. - On
Department, select Change Behavior Order and set the order:Reset project list, then the Filter, thenCheck project list. - If a project is required, do not put Required on
ProjectCode: it blocks the user while the list is empty. Use the Alternate Requirement Behavior onProjectCodewithProjectCodeManualas the other field, so either one satisfies the rule.
Scripts¶
// Attach to: Department. Order: FIRST, before the Filter behavior.
// Writes: choices of ProjectCode.
// Empties the list, so a failed lookup cannot leave the previous department's projects.
builder.SetFieldChoices("ProjectCode", []);
// Attach to: Department. Order: LAST, after the Filter behavior.
// Reads: Department, choices of ProjectCode, ProjectCodeManual.
// Writes: visibility, message and value of ProjectCodeManual.
var department = (panel["Department"].RuntimeValue || "").toString();
var projects = panel["ProjectCode"].Choices || [];
var manual = (panel["ProjectCodeManual"].RuntimeValue || "").toString();
var listMissing = department !== "" && projects.length === 0;
builder.SetFieldIsVisible("ProjectCodeManual", listMissing);
// true also clears the old notice on ProjectCodeManual
builder.SetFieldIsValid("ProjectCodeManual", true);
if (listMissing) {
builder.SetFieldValidationMessage("ProjectCodeManual",
"No projects were found for this department. Type the project code here.");
} else if (manual !== "") {
builder.SetFieldRuntimeValue("ProjectCodeManual", "", true);
}
How it works¶
- Behaviors on one field run in order, and each waits for the one before it. Script 1's empty list is in place when the Filter runs.
- When the query works, the Filter replaces the list. When it fails, the list stays empty, and the Filter shows "Could not retrieve data from source." on
Department. - Script 2 reads
Choicesafter the Filter. A department with an empty list showsProjectCodeManualwith a notice. The field stays valid, so the notice informs and does not block. - When the list loads again, Script 2 hides
ProjectCodeManualand clears any value typed there.
Test it¶
- Save, then open a document in the Index app. Pick a department that has projects.
ProjectCodelists them andProjectCodeManualis hidden. - Pick another department. The list changes to that department's projects.
- Add a test choice to
Departmentthat has no rows in the source table, and pick it.ProjectCodeManualappears with the notice. - To test a failed query, use a copy of the content type whose Filter points at a table that does not exist. Pick a department from step 1 first, then any department. The dropdown is empty, not left on the first department's projects.
You are done when a department with no projects shows ProjectCodeManual with the notice, the dropdown is empty instead of showing another department's projects, and a working department hides the manual field again.
What can go wrong¶
Watch out
- The manual field never shows on a failure. Script 1 runs after the Filter, or is missing. The old list is still there. Fix Change Behavior Order.
- The manual field shows for every department. Script 2 runs before the Filter. Move it last.
- The list fails for some values only. SQL lookups can be case-sensitive. Normalize the key first: see Format and Normalize Field Values.
- Every department fails. Check the Filter's source connection, table and column names. A gateway that is offline fails every lookup.
ProjectCodeis cleared. When the query works but returns no rows, the Filter itself clearsProjectCode. When the query fails, the stored value stays. The scripts never changeProjectCode's value.- Server runs. Run Indexing Behaviors and Start QC Task run the same chain. If the source fails there, the scripts change nothing that is stored, except clearing
ProjectCodeManualwhen the list did load.
Variations¶
Send manual codes to review. In the workflow, route work items where ProjectCodeManual has a value to a review step, so someone checks codes that were not picked from the list.
Related¶
- Dynamic Dropdown Filtering: a short list kept in the script, with no source.
- Compound Cascading Filter
- Debugging Custom Index Behaviors
- Custom Index Behavior Reference