← servoy magazine

[Tip] Basic JavaScript in Servoy Methods

by Bob Cusick—ClickWare

Servoy's scripting language is based on JavaScript - so it follows the object model of JavaScript, where you reference the objects you want to script.

For example, setting the value of a field on a form (assumes we have a form called "Customers"):

Form.Field = Value
Customers.FirstName = 'Bob'

It's the same when you're setting the properties of an object:

Form.Object.Property = Value
Customers.FirstName.enabled = FALSE

SAME when you're performing an action (or FUNCTION):

Form.Function
Customers.deleteRecord

All JavaScript Ifs and Loops must be contained within curly brackets:

If ( a == b ) {
    DO STUFF HERE
}

Another thing to get used to is the fact that IF statements DO NOT use an "End If" - the closing curly bracket is the same thing as an "End If". You can do an ELSE statement like this:

If ( a == b ) {
    DO STUFF HERE
}
Else {
    DO OTHER STUFF HERE
}

LOOPING is VERY, VERY different than FileMaker. First of all - it uses a FOR statement, and you specify a variable, then increment the variable. A simplified, written out version would look something like this:

For x = 1 to 10
    DO STUFF
    x = x+1
End

In JavaScript, you can specify the loop AND increment it (i.e. The x = x+1
step) ALL AT ONCE:

for ( var x = 0; x < 10; x++ ) {
    DO STUFF
}

Remember, all functions have context sensitive help to give you a leg up on learning the syntax. At the top of the function window, there is a button to "move sample" when a function is highlighted. Clicking it will move a fully documented sample of code and it's use for the particular function.