sfSetDefaultValue

Sets the provided value to a form control (only if the field is empty).

This function is a variation of sfSetValue and is intended to set a default value to form controls. If the field already has a value, no action is done. If the field is empty, then the provided default value is set to the field.

Syntax

sfSetDefaultValue (id, value, index, skipChangeEventTrigger)

Parameters

Name

Type

Mandatory

Description

Name

Type

Mandatory

Description

id

string

yes

Control id of the form control.

value

any

yes

Default value to be set to the form control.

It must be a value that is supported by the form control. See values supported by form controls.

index

number

no

Index of the form control. Applicable only if the control is present inside a group control.

The index is a zero-based index.

skipChangeEventTrigger

boolean

no

Indicates if the change event trigger should be skipped.

When a new value is set to a form control or the existing value in the control is changed, a change event is triggered by default that evaluates all runtime expressions that are either bound to or depend on the control. This parameter can be used to prevent the change event so that the runtime expressions are not re-evaluated.

It is generally not recommended to set this parameter to true as it may lead to inconsistency in data. However, it may be useful to prevent issues that may arise due to cyclical dependencies in runtime expressions.

Applies to

Control type

Applicable

Remarks/Exceptions

Control type

Applicable

Remarks/Exceptions

Input controls

Yes

Applies to all input controls except Bar/QR code scan, Calendar, Form, Identifier, Image and Signature.

Layout controls

No

 

Behaviour controls

No

 

Static information controls

Yes

Applies to HTML only.

Value accepted by the function differs with the form control. See values supported by form controls.

Usage examples

Example 1

Let us assume a form contains a text field TankNumber. To set a default value to the text field,

sfSetDefaultValue('control_tanknumber', 'TK010')

Example 2

Let us assume a form contains a group control QuestionsChecklist with a numeric field CapacityInLitres. To set a default value to the numeric field in each row,

for(var i = 0; i < sfField('div_questionschecklist').recordCount(true); i++) { sfSetDefaultValue('control_capacityinlitres', '100', i) }

Example 3

Let us assume a form contains a selectbox control Terminal that displays the list of terminals from a dataset Terminals. The dataset has properties TerminalName and TerminalContactEmail, in addition to Code that is the default property available to all dataset items.

The selectbox displays TerminalName in the dropdown. To select a terminal as default in the dropdown, the Code of the intended terminal should be set as its value. So,

sfSetDefaultValue('control_terminal', 'BPMEX')

As a result, the terminal with Code BPMEX is selected in the selectbox.

Example 4

Let us assume a form contains a file control Reference. A user handbook PDF has been uploaded as a resource named UserHandbook. To bind the user handbook by default to the file control,

As a result, the PDF uploaded as a resource is shown as a file in the field Reference.

Supported value formats

The function accepts value in different formats depending on the form control.

Form control

Format

Remarks

Form control

Format

Remarks

Checkbox

Set 0: 0 or 1
Set 1: OK
Set 2: OK or NOK
Set 3: OK or NOK or NA
Set 4: OK or NOK or NA

Accepts one of the select options (depending upon the set used)

Date

YYYY-MM-DD

Accepts a valid date

Date Time

YYYY-MM-DD HH:MM

Accepts a valid date and time (in 24-hour format)

HTML

 

Accept a valid HTML text

File

Resource name

Accepts the name of a resource. The resource must be a supported file type and must present in the domain

Multiple files from resources can be assigned by providing a comma separated list of resource names.

Map

 

Accepts a valid coordinates (as comma separated latitude and longitude in decimal degrees)

Multiple choice

 

Accepts the code of the dataset item to be selected

Multiple items can set by providing a comma separate list of codes.

Numeric

 

Accepts a valid numeric value

Numeric spinner

 

Accepts a valid numeric value

Radio

 

Accepts the code of the dataset item to be selected

Selectbox

 

Accepts the code of the dataset item to be selected

Slider

 

Accepts a valid numeric value

Text

 

Accepts a text

Textarea

 

Accepts a text

Time

HH:MM

Accepts a valid time (in 24-hour format)

Toggle

0 or 1

Accepts the state of the toggle (0 for off and 1 for on)