Skip to content

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, CustomerName and VendorId (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

  1. In the Connections app, open the content type.
  2. For each script, select the field it formats, open Behaviors > Add a Behavior > Custom Behavior, paste the script, and select Save & Close.
  3. 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 metadataKvp and writes it back with SetRuntimeValue, 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 VendorId script runs before the lookup on the same field, so the lookup searches for AB-1001, not ab-1001 with a trailing space.

Test it

  1. In the editor, select Test. Type 555.123.4567 in PhoneNumber and leave the field. It shows (555) 123-4567.
  2. Type 12345. The field turns red with the message. Type a 10-digit number. The message clears.
  3. Type MARY O'BRIEN in CustomerName. It shows Mary O'Brien.
  4. 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. SetRuntimeValue writes 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 McDonald or van der Berg come 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.