FSM Expressions and Template Tags

Note:

This topic is intended for administrators and developers who use expressions and template tags in configuration for Field Service Management (FSM) Mobile.

Use expressions and template tags to provide configuration values that depend on other data. FSM Mobile expressions can calculate element values and change element behavior during form completion. Template tags can insert data into a resource definition before FSM Mobile renders it.

For file imports and independent FSM Mobile page customization, see FSM Mobile Scripting and Customization.

This topic covers:

Choosing a Value Method

Choose a method based on when the value is determined and whether it must respond to changes in FSM Mobile. The following table compares the available methods.

Method

Purpose

Example

When the Value Is Determined

Static value

Supplies a fixed configuration value.

"required": true

When the configuration is used.

FSM Mobile expression

Calculates an element property from element data.

"required": "completed.value"

When the element is initialized, subject to the saved-value rules, and when a referenced local element property changes.

Template tag

Supplies a value from the context available when a resource definition is prepared.

"label": "Job ${ event.internalid }"

When the resource definition is prepared. It doesn't create a local listener for subsequent changes to an FSM Mobile element.

For the properties and accepted values of each element type, see FSM Mobile Element Reference. For the properties of a resource definition, see FSM Mobile Resource Properties Reference.

Defining Mobile Expressions

An FSM Mobile expression is a JavaScript expression written as a JSON string in an element property. It must return a value accepted by that property and element type.

For example, the following element-definition fragment makes a signature visible and required when you check the Completed box:

            {
  "completed": {
    "type": "checkbox",
    "label": "Completed",
    "value": false
  },
  "approvalSignature": {
    "type": "signature",
    "label": "Approval Signature",
    "hidden": "!completed.value",
    "required": "completed.value"
  }
} 

          

Place an element-definition fragment within the element object of the appropriate routes/mobile resource. The property names in the fragment identify the elements.

Expression Rules

When writing an expression:

  • Use one expression that produces a value. Don't begin it with return or use a sequence of statements.

  • Refer to an available element property with dot notation, such as quantity.value or this.value.

  • Include a recognized element-property reference. A string containing only a calculation or a function call doesn't, by itself, establish an FSM Mobile expression binding.

  • Keep structural properties, including an element's type, name, parent, URI, and column definition, fixed. Use expressions only for properties that support expressions.

  • Return the correct value type. A string such as "false" is not the Boolean value false.

  • Handle empty values explicitly. Avoid results such as undefined, NaN, or infinity.

  • Avoid circular dependencies, such as two calculated elements whose values depend on each other.

  • Keep calculations free of record writes, network calls, and other side effects. An expression can run repeatedly during work on a form.

When creating element names for expressions, start with a letter or an underscore, followed by letters, digits, or underscores. Avoid spaces, punctuation, and JavaScript reserved words. Use names consistently, including letter case.

Use dot notation for the element-property reference even if the rest of the expression uses array access or other JavaScript syntax. For example, readings.value[0] identifies readings.value as the input. The reference readings['value'] doesn't provide the same recognized reference.

Returning Accepted Values

The expression result must match the accepted static value for the property. The following table illustrates common cases. It doesn't replace the property reference.

Property or Use

Required Result

Example

hidden, required, disabled, readonly, or Select multiple

Boolean

completed.value

A label or a Text value

String

completed.value ? 'Complete' : 'In progress'

A Number value

Number, or an empty value accepted by the element

quantity.value === null ? null : quantity.value * 2

Select options

Array of option objects

services.options

Single-select value

One selection object, or null

useDefault.value ? this.options[0] : null

Multiple-select value

Array of selection objects, or an empty array

useDefault.value ? this.options : []

A Date, Time, or Date-Time value

A string in the required format, or an empty string

useCurrentDate.value ? now().date : ''

The Select examples assume that the referenced options exist and contain the required option properties. Don't reference this.options[0] unless the options array has an entry.

For the effect of changing a Select's options on its current selection, see Select Defaults and Stored Values.

Changing Select multiple doesn't provide a general conversion between a single selection object and an array of selections. Supply a compatible value when changing selection mode.

For example, the following fragment switches between single-selection and multiple-selection modes when Allow Several changes. Its value expression returns null when the Allow Several box is cleared and an empty array ([]) when the box is checked. These represent no selection in the respective modes, so switching modes clears any existing selection:

              {
  "allowSeveral": {
    "type": "checkbox",
    "label": "Allow Several",
    "value": false
  },
  "chosenServices": {
    "type": "select",
    "label": "Services",
    "autoselect": false,
    "multiple": "allowSeveral.value",
    "value": "allowSeveral.value ? [] : null",
    "options": [
      { "id": "inspection", "label": "Inspection" },
      { "id": "repair", "label": "Repair" }
    ]
  }
} 

            
Warning:

Changing the selection mode in this example clears the current selection. If selections must be retained, define the required conversion.

Expression Evaluation and Saved Values

Expressions depend on referenced element properties. When a referenced local property changes, FSM Mobile evaluates the expression again and applies its result to the target property.

For example, this fragment calculates a line total when Quantity or Unit Price changes:

            {
  "quantity": {
    "type": "number",
    "label": "Quantity",
    "min": 0,
    "value": 1
  },
  "unitPrice": {
    "type": "number",
    "label": "Unit Price",
    "min": 0,
    "value": 0
  },
  "lineTotal": {
    "type": "number",
    "label": "Line Total",
    "readonly": true,
    "value": "quantity.value === null || unitPrice.value === null ? null : Math.round(quantity.value * unitPrice.value * 100) / 100"
  }
} 

          

During initialization, FSM Mobile handles value expressions differently from expressions on other properties:

  • If an element has no supplied record value, its value expression can provide the initial value.

  • If an element already has a supplied record value, FSM Mobile preserves that value when opening the record. It doesn't automatically replace it with a fresh calculation.

  • A subsequent change to a referenced local property can recalculate the value and replace the preserved value.

  • Expressions on other properties, such as hidden and required, are evaluated during initialization.

Setting readonly to true prevents the user from editing the element directly. It doesn't force a stored value to be recalculated when an existing record opens.

If a calculated value is overridden, changing a referenced local property can replace that override.

Referencing Element Data

An element property can refer to another available element's properties or to properties of the current element.

Reference

Meaning

Example

elementName.property

A property of an available element.

quantity.value

this.property

A property of the element containing the expression.

this.value

A shared element URI followed by a property

Data from an element registered for sharing.

services.options

Use this to refer to another property of the current element. For example, avoid referencing this.value in a value expression because that property would depend on itself.

Table Elements

Within a table row, child-element references use the values from that row. A quantity column and a price column can therefore calculate a total column independently for each row.

A top-level expression can use a table's value, which is an array of row data. Check for an empty array before accessing a row, and use the actual child-element names when reading row values.

Don't assume that a child element can directly refer to an unrelated child in another row or to every element outside its table. Use references within the same row or an explicitly shared element. Confirm other cross-scope arrangements against the FSM Mobile version used by the account.

Sharing Data Between Resources

Use a shared element's registered uri followed by the required property. For example, services.options reads the option list registered under the URI services. It uses the URI, which can differ from the element's name or the supplying resource's ID.

The supplier must be loaded before the reference can be used. For complete supplier and consuming Select examples and resource-loading rules, see Sharing Data Between Resources. Avoid URI names that conflict with unrelated local element names.

Shared data must be available when the consuming expression runs. Don't assume that updating a different resource will immediately recalculate an already open form. After refreshing the supplying data, reopen the consuming resource to use it.

Reusing Calculations

Custom element properties can hold intermediate values for use by other expressions. These properties contain calculation data rather than additional standard element controls and are not automatically NetSuite fields.

The following fragment uses an intermediate Boolean property to calculate a result label:

            {
  "reading": {
    "type": "number",
    "label": "Reading",
    "value": null,
    "limit": 100,
    "overLimit": "this.value !== null && this.value > this.limit"
  },
  "readingResult": {
    "type": "text",
    "label": "Result",
    "readonly": true,
    "value": "reading.value === null ? '' : (reading.overLimit ? 'Over limit' : 'Within limit')"
  }
} 

          

Avoid names that conflict with standard element properties. For larger calculations used by several forms, an imported JavaScript function can provide one implementation. For expression use, see Using Imported Functions and Data. For file setup, see FSM Mobile Scripting and Customization.

Removing Duplicate Array Values

The unique(array) helper returns an array containing the first occurrence of each distinct value. An optional second argument identifies a property to compare when the entries are objects, for example unique(this.options, 'id') in a separate custom property. Without that argument, object entries are compared by identity rather than by matching all their contents.

The expression must still contain an available element-property reference, and the result must be accepted by the target property.

Expression Logging

The "routes/mobile".log property controls expression messages in the browser console. The default value is false. Set log to true to turn on logging.

            {
  "routes/mobile": {
    "log": true
  }
} 

          

The following table describes element-binding messages.

Message

Meaning

BIND INIT

An expression and its dependencies are initialized.

BIND SET

An evaluated result is assigned.

BIND PARSE ERROR

The expression could not be parsed.

BIND RUNTIME ERROR

The expression failed during evaluation.

Other evaluation paths can report ExpressionError. Set log to false to turn off these messages. For browser request capture, see Capturing FSM Performance Data.

The following example shows the default log value:

            {
  "routes/mobile": {
    "log": false
  }
} 

          

Additional Information

Related Topics

General Notices