Default a Filter to the Current User¶
A claims team shares one "My open claims" search. Each adjuster should see their own claims as soon as the search opens, without typing their name, and still be able to look up a colleague's claims.
At a glance
- Extension point Search filter behavior
- You need A saved search with an
Assigned Totext filter (Textbox) on a property that stores user names in the same form ascontext.username; the Create/Share Saved Searches permission - Teaches
context.username, filling a filter only when it is empty, exact filter names
Set it up¶
- In the Search app, open the Builder and open the saved search.
- Select the
Assigned Tofilter, then Customize Filter. - On the Behaviors tab, select Not Configured next to Custom Behavior.
- Name the behavior
Default to me, paste the script below, select Test, then Save & Close. - Save the search.
This does not work on an identity property (a people field). An identity filter allows only the default value behavior.
Script¶
Search filter behavior on Assigned To
// Attach to: Assigned To (a text filter)
// Reads: Assigned To, context.username. Writes: Assigned To.
// The user sees their own user name in the filter when the search opens.
var current = (panel["Assigned To"].Value || "").toString();
if (current === "" && context.username) {
builder.SetFilterValue("Assigned To", context.username);
}
How it works¶
- The panel load runs every behavior, so the script runs when the search opens.
"Assigned To"is written as one quoted string, exactly as the Builder shows it. The editor swaps it for the filter's internal key when you save.- The script writes only when the filter is empty, so a name the user types stays.
SetFilterValuedoes not run the filter's own behaviors, so the script does not loop.
Test it¶
- In the behavior editor, select Test. The preview panel shows your user name in
Assigned To. - Save, then open the search from the Search app as two different users. Each sees their own name.
- Type a colleague's name and run the search. The typed name stays and the results change.
You are done when each user opens the search with their own name in Assigned To and gets their own items.
What can go wrong¶
Watch out
- The filter stays empty. The name in the script does not match the filter name exactly, or it is built from pieces. Write
"Assigned To"as one quoted string, with the same case. - The search finds nothing. The stored values are in a different form from
context.username, for example a display name in place of a sign-in name. Compare the value the Test preview shows with a stored value. - The filter fills again after the user clears it. The script runs each time the filter changes, and an empty filter is filled again. Users who want everyone's items need a separate saved search.
- Every behavior on the panel fails for one user. The user name contains an apostrophe. The query panel does not escape it. There is no script-side fix.
- "could not be fired" on the filter. The script threw an error or ran longer than 1.5 seconds. See Debugging Search Filter Behaviors.