Form scripts
It is possible to write small scripts that update the form values. The script is run whenever you change a field in the form whilst completing the form. This is a functionality that has to be enabled by Microbizz. For details about script see this.
When you setup the form you can specify if a field should be updatable from the script:
Editing the script
The script is edited in the SCRIPT tab, where you can also try out the form:
When the script is run a variable named a
is set up to hold all the current values from the question form.
You may update the variable, and the question form will then be updated with the new values. To update the field with ID 5 you write a new value to a[5]
. You access the current values the same way: a[3]
will return the current value of the field with ID 3. You can see the ID of a field by holding the mouse over the field, the ID will appear in the tooltip.
The variable this
refers to the object on which the question form is being completed, f.ex. a task. This is called the context and is described here. If f.ex. the context is a task, then this.customer
is the customer for the task.
The variable texts
holds the text for each of the questions, see details below.
Field types
You can update most field types, except for files, pictures and signatures, but including the static text fields.
Notice that when you set the value for a radio / several of many / one-of-many field, you should not specify the text that is displayed to the user, but instead specify the underlying value used by Microbizz. F.ex. for the field below you might specify
a[7] = 2
instead of
a[7] = "Ringo"
For multi fields you have to specify a list of values to select, eg.: a[8] = [2,5,1]
.
Texts
The texts for the questions are available in the variable texts
, so in the above example texts[7]
holds the value Person
. You cannot change the texts.
Bugs
Accessing arrays
There is a known bug which complicates reading values from “several of many” fields. The bug is difficult to fix so you will have to work around the problem.
When the script is executed for a form that includes a “several of many” field, the value for the field is an array if one or more options have been selected by the user, eg. a[9]
may be set to [1,2]
if the second and third options are selected (first option is number 0!). There are a bunch of useful functions for examining / manipulating this array, like index()
and slice()
but these do not work as long as you access the array directly via a[9]
. Instead you must copy the array to a new variable and then you can use index()
or slice()
.
Example
// doesn't work
if a[9].index(1) >= 0
a[10] = "You have selected item 1"
// works
a9 = a[9]
if isarray(a9) && a9.index(1) >= 0
a[10] = "You have selected item 1"
Feedback
The script may report back to the user by using the function echo()
.
F.ex.:
echo("Remember to inform the accounting department")
This text will be shown to the user just like many other informations: