Format and Normalize Field Values¶
Indexers type the same value in many forms: 555.123.4567, (555) 123 4567, ab-1001 with a trailing space. Reports, matching and lookups need one stored form. Each script on this page rewrites its own field as the user leaves it, and marks the field invalid when the value cannot be formatted. The last script normalizes a key before a lookup behavior reads it, because source lookups can be case-sensitive.
At a glance
- Extension point Custom Index Behavior
- You need Textbox fields
PhoneNumber,CustomerNameandVendorId(a key with a lookup behavior); permission to edit content types in the Connections app - Teaches Rewriting the attached field with
SetRuntimeValue, validating in the same script, running a Custom Behavior before a lookup with Change Behavior Order
Set it up¶
- In the Connections app, open the content type.
- For each script, select the field it formats, open Behaviors > Add a Behavior > Custom Behavior, paste the script, and select Save & Close.
- For
VendorId, select Change Behavior Order and drag the Custom Behavior above the lookup behavior (Automatic Indexing, Source Lookup or Source Validation). Behaviors on one field run in that order, and each sees the value the one before it wrote.
Format Textbox fields only. Currency, NumericUpDown and DateTime fields have their own value formats, and a formatted string can break them.
Scripts¶
Custom Behavior on PhoneNumber
// Attach to: PhoneNumber. Stored form: (555) 123-4567
// Reads and writes: PhoneNumber.
var raw = (metadataKvp.RuntimeValue || "").toString();
var digits = raw.replace(/\D/g, "");
var ok = digits.length === 0 || digits.length === 10;
builder.SetIsValid(ok);
if (!ok) {
builder.SetValidationMessage("Enter a 10-digit phone number.");
} else if (digits.length === 10) {
var formatted = "(" + digits.substring(0, 3) + ") " + digits.substring(3, 6) + "-" + digits.substring(6);
if (formatted !== raw) {
builder.SetRuntimeValue(formatted);
}
}
Custom Behavior on CustomerName
// Attach to: CustomerName. "mary o'brien-smith" becomes "Mary O'Brien-Smith".
// Reads and writes: CustomerName.
var raw = (metadataKvp.RuntimeValue || "").toString();
var formatted = raw.replace(/\s+/g, " ").trim().toLowerCase().replace(/(^|[\s'-])([a-z])/g, function (match, before, letter) {
return before + letter.toUpperCase();
});
if (formatted !== raw) {
builder.SetRuntimeValue(formatted);
}
Custom Behavior on VendorId, ordered before the lookup
// Attach to: VendorId. Order: before the lookup behavior on VendorId.
// The source table stores keys in uppercase with no spaces at either end.
// Reads and writes: VendorId.
var raw = (metadataKvp.RuntimeValue || "").toString();
var normalized = raw.replace(/^\s+|\s+$/g, "").toUpperCase();
if (normalized !== raw) {
builder.SetRuntimeValue(normalized);
}
How it works¶
- Each script reads its own field through
metadataKvpand writes it back withSetRuntimeValue, which never re-runs the field's own behaviors. The scripts cannot loop. - Each script writes only when the result differs, so opening a correct document changes nothing.
- The phone script calls
SetIsValid(ok)first, then adds a message only when the value is wrong. An empty value passes. Add a Required behavior if the field must have a value. - The
VendorIdscript runs before the lookup on the same field, so the lookup searches forAB-1001, notab-1001with a trailing space.
Test it¶
- In the editor, select Test. Type
555.123.4567inPhoneNumberand leave the field. It shows(555) 123-4567. - Type
12345. The field turns red with the message. Type a 10-digit number. The message clears. - Type
MARY O'BRIENinCustomerName. It showsMary O'Brien. - Save. In the Index app, type a known vendor key in lowercase with a trailing space in
VendorId, then leave the field.
You are done when VendorId shows the uppercase key and the lookup fills the related vendor fields.
What can go wrong¶
Watch out
- The value never changes.
SetRuntimeValuewrites only to the attached field. Check that the script is on the field it formats. - The lookup still finds nothing. The Custom Behavior runs after the lookup. Fix Change Behavior Order. If the source mixes cases, fix the source instead: a view with
UPPER(...)on the key column makes every lookup consistent. - The source stores another form. Match it. If keys are stored in lowercase, use
toLowerCase(). Remove dashes or leading zeros only if the source does too. - "Could not retrieve data from source." under the field. That message comes from the lookup behavior, not from the script. See Handle a Failed Lookup in a Cascading Dropdown and the debugging page.
- Names such as
McDonaldorvan der Bergcome out wrong. Title case has exceptions. Do not auto-format names where exact spelling matters. - "Could not find localized string." The script called
SetValidationMessage("").SetIsValid(true)clears messages.