Skip to content

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 fills ProjectCode; 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 Choices after 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

  1. In the Connections app, open the content type.
  2. Select ProjectCode and remove its static choices. The Filter fills the list.
  3. Select Department. Add a Custom Behavior named Reset project list with Script 1.
  4. On Department, check the Filter behavior: its Destination Column is ProjectCode.
  5. On Department, add a second Custom Behavior named Check project list with Script 2.
  6. On Department, select Change Behavior Order and set the order: Reset project list, then the Filter, then Check project list.
  7. 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 on ProjectCode with ProjectCodeManual as the other field, so either one satisfies the rule.

Scripts

Script 1: Custom Behavior on Department, first
// 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", []);
Script 2: Custom Behavior on Department, last
// 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 Choices after the Filter. A department with an empty list shows ProjectCodeManual with a notice. The field stays valid, so the notice informs and does not block.
  • When the list loads again, Script 2 hides ProjectCodeManual and clears any value typed there.

Test it

  1. Save, then open a document in the Index app. Pick a department that has projects. ProjectCode lists them and ProjectCodeManual is hidden.
  2. Pick another department. The list changes to that department's projects.
  3. Add a test choice to Department that has no rows in the source table, and pick it. ProjectCodeManual appears with the notice.
  4. 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.
  • ProjectCode is cleared. When the query works but returns no rows, the Filter itself clears ProjectCode. When the query fails, the stored value stays. The scripts never change ProjectCode'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 ProjectCodeManual when 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.