Scripting Field values
Formula field
Description: use to perform calculations using values input in the Form. E.g. enter Width and Height in the Form and output the Surface in the Form View.
Where to enter code: Template Field Edit in web account / Script field
Syntax: any valid Javascript. You can prefix the code with a = and define a function.
Example:
Field F1 is a Width, F2 is a Height. Formula field calculates the Surface:
(F2*F1).toFixed(2) + ' square meters'
Field F1 is a Width, F2 is a Height. Formula field calculates the Surface using a function:
=computeSurface();
function computeSurface() {
return (F2*F1).toFixed(2) + ' square meters'
}
Default Value of a Field:
Description: a preset value for the field, either a literal value or a variable evaluated using Javascript.
Where to enter code: Template Field Edit in web account / Default Value field
Syntax: start with a =.
Examples: set a Date field to Today's date; set an Address with a linked Contact's address
=Date.today();
=(link!=null) ? link.street + link.city : "";
Options of a Combo Box Field:
Description: set list of options of a Combo Box from datasets. (N.B.: for hard-coded options see:
Edit Field Options)
Where to enter code: Combo Box type of Field Edit in web account / Options field
Syntax: start with a =.
Example: get options from a Dataset name "My First Dataset". Datasets can be created from the
Options menu.
=Forms.datasetOptions("My First Dataset");
Onchange attribute of a Field:
Description: code that is executed when the value of the field is changed in the Form Edit screen.
Where to enter code: Template Field Edit in web account / Onchange field
Syntax: you can access the new value of the field in the
'value' variable. The
'formid' variable contains the unique identifier of the current form. The code must return true or 1 for the field value to be updated.
Example: validation of a selected value in a Combo box
if (value == 'Blue') { App.alert('Wrong color selected');return false;} else {return true;}
Syntax: Use the
_valueObj global variable to update one or more field values. _valueObj is the object corresponding to the JSON representation of the form field values.
Example: update the value of fields F2 and F3 with the new field value
_valueObj['F2'] = value + ' copied to F2';
_valueObj['F3'] = value + ' copied to F3';
var values = JSON.stringify(_valueObj);
Query.updateId("Forms.forms", formid, 'value', values);
Example: Change the end date field F2 based on the value of start date field F1:
var d = new Date(new Number(value));
var value2 = d.setFullYear(d.getFullYear()+1);
_valueObj['F1'] = value;
_valueObj['F2'] = value2;
var values = JSON.stringify(_valueObj);
Query.updateId("Forms.forms", formid, 'value', values);
History.reload();
return 0;