...
F.ex. you may want to have a custom field which displays the average of the values in 2 other custom fields, but only include the values that are larger than 1000. This can be done with the following script:
Code Block |
---|
n = 0 |
...
sum = 0 |
...
if this.custom200 > 1000 |
...
n++ |
...
sum += this.custom200 |
...
if this.custom201 > 1000 |
...
n++ |
...
sum += this.custom201 |
...
if n == 0 |
...
return "" |
...
return sum / n |
Example 1 - average of custom fields with values > 1000
...
Blocks are defined by being indented by 2 SPACEs. Other languages use curly brackets { } to define blocks. A block ends at the next line with less indentation, or at the end of the script:
Code Block |
---|
if this.custom200 > 1000 |
...
n++ |
...
if this.custom201 > 1000 |
...
if this.custom202 > 1000 |
...
n++ |
...
else n-- |
Example 2 - indentation
Functions
Functions are defined by the keyword def
, followed by the function name, followed by the arguments in brackets; the function body must be indented. A function may return a value, using the keyword return
.
Code Block |
---|
def positive_sum(a,b,c) |
...
sum = 0 |
...
if a > 0 |
...
sum += a |
...
if b > 0 |
...
sum += b |
...
if c > 0 |
...
sum += c |
...
return sum |
Example 3 - simple function
...
Also, // or # after an expression also indicates that the rest of the line is a comment.
Code Block |
---|
// example of comments |
...
a = 1 // this is a comment |
...
b = 2 # this is also a comment |
Example 4 - comments
Variable types
...
The if
-elseif
-else
and while
control structures are supported. while
supports break
and continue
.
Code Block |
---|
res = 0 |
...
if this.custom200 < 1000 |
...
res = this.custom200 |
...
elseif this.custom201 < 1000 |
...
res = this.custom201 |
...
else |
...
res = 1 |
...
while res > 2 |
...
res /= 2 |
Example 5 - control structures
Loops can be made using the for
keyword. This iterates through the values in some type of iterable value, which can either be a the result of the range()
function, or a string or array or dict. for
loops support continue
and break
.
Code Block |
---|
for i in range(1, 100) |
...
do_something(i) |
...
for letter in "ABCDEFG" |
...
do_something(letter) |
...
for prime in [1, 2, 3, 5, 7, 11, 13] |
...
do_something(prime) |
...
dictionary = {a: 1, b: 2, c: 3} |
...
for key in dictionary |
...
do_something(key, dictionary[key]) |
Example 6 - for loops
Variable scope
Variables that are created outside a function are global. To use a global variable from inside a function, you need to use the global
keyword to specify the variable:
Code Block |
---|
myname = "Microbizz" |
...
mytitle = "Mr." |
...
def getmyname() |
...
global myname, mytitle |
...
return "My name is "+myname |
Example 7 - global variables
...
When you pass variables as arguments to a function, scalar/primitive variables (strings and numbers) are passed by value, whereas arrays/dicts/objects are passed by reference. This means that the function may modify arrays/dicts/objects that are passed, but not strings and numbers.
Code Block |
---|
myname = {name:"Microbizz"} |
...
mytitle = "Mr." |
...
changename(myname, mytitle) |
...
// myname.name may now have changed, but mytitle is the same |
Example 8 - pass by reference
...
When the script is run to generate the value for a custom field, the return value is specified by the keyword return
outside of a function, this will also stop execution of the script; the return value is what is displayed in Microbizz.
Code Block |
---|
area = "nowhere" |
...
if this.postcode >= 1000 && this.postcode <= 2999 |
...
area = "capital" |
...
elseif this.postcode < 9999 |
...
area = "countryside" |
...
return area |
Example 9 - return value
The return value may either be text, as in the example above, or it may be HTML generated by the HTML
object.
...
Microbizz logs various script related things in the System log.
Code Block |
---|
Microbizz.Log("The script was started") |
...
// first argument should be a string, second argument is optional and may be an array or dict or string or number |
...
Microbizz.Log("Some data", [123,"ABC"]) |
Example 10 - logging
Sandbox
...
F.ex. there may be complicated pricing calculations that depend on the date and postcode of a task. This might be developed by Ventu and could then become available as a new script object providing access to the calculations. In the unrealistic example below, a new Tax
object provides two new functions:
Code Block |
---|
taxrule = Tax.getRule(this.zip, this.createdate) |
...
return Tax.calculate(this.price, taxrule) |
Example 11 - unrealistic extension
...
This requires that the custom fields are marked as "show in app". We assume that the company custom field has id=321 and the task custom field has id=123.
Code Block |
---|
cus = this.customer |
...
if cus.indb() |
...
this.sv("custom123", cus.custom321) |
...
this.save() |
Example 12 - copy custom field
...
This requires that the custom field is marked as "show in app". We assume that the custom field has id=321.
Code Block |
---|
if this.tasktype == 5 && this.parenttodo == 0 |
...
subtodos = this.subtodos |
...
if isarray(subtodos) |
...
for id in |
...
subtodos todo = Microbizz.GetTodoByID(id) |
...
todo.sv("custom322", this.custom321) |
...
todo.save() |
Example 13 - copy custom field
...
If an array holds a dict as an element, then accessing an element in the dict using eg.
Code Block |
---|
value = arrayname[0].dictelement |
will not work ; you should use something like
Code Block |
---|
...
dictname = arrayname[0] |
...
value = dictname.dictelement |
2)
Some syntax errors are not detected/reported.