Code blocks

HOW TO

Make new variable

Variable is a container for different types of data (Integer, String, Boolean, etc).

In the data panel, click the Make variable button.

Select the type you need. Click on it to set the name. Alternatively, you can drag the type name onto the working area. In this case, a variable with a default name (like “i” or “j”) will be created.

Give a name to a variable, and click Create.

Set the value of the variable

Set block like  is used to assign a value to a variable. For example, in Java, assignment operation looks  like:
Age = 10;

In the code blocks:

In the Web Pages Editor we have created a field Age. It is referred to as item.Age in the Code Blocks. Drag a variable from the data panel across the green SET area to create a set Block.

Note: you need to connect the Block to the action body.

To get a set Block, you can right click on a variable in the working area and select “Set variable” from the context menu.

Get the value of a variable

Use Get block  to access the value of a variable.

For example, the expression:

a = b + c;

In code blocks looks like:

To get a variable, you can drag a variable from the Data panel and move it across the grey GET area.

You can right click on any variable in the working area and select the “Get variable” option from the context menu to obtain the Get block (access its value).

Use fields from Web pages editor

The fields you create in the Web pages editor can be used in the code blocks like any other variables.

To find them, you can use either the Search box or unfold item in the Data Panel on the right. Item is an action parameter variable  containing the current page object. Unfold item variable and select any of its field members.

Keep in mind that you can see the variable only if you have one of the Show all or Variables section opened.

Drag fields onto the working area and use them as dictated by your code logic.

How to create IF statement

The IF block evaluates the given condition and, if the result is true, executes an inner block of code.

You can simply drag the If - do block from the blocks panel, LOGIC section.

The following code prints the success message “Allowed!” IF the Age is bigger than 18.

If you need to add the else statement, do the following. Click on the blue statement options icon (located at the top left corner of if block) and attach else statement like shown.

How to create for loop

To repeat the same code multiple times, repeat blocks are used.

The loop below runs info(“Hello world”) three times:

Considering repeat while, while the condition is truthy, the code from the loop body is executed.

Note: be aware of infinite loops!

Create function

To create a function, go to the data type details page and from the Code Blocks section, select Function.

Then give it the name and select whether it should be run on Server or on the Client.

The function, created in one data type, can be used anywhere in that builder.

Add parameters to a function

Click on the gear icon located to the left of a function name. Drag the input block into the provided panel. You can rename parameters there, too.

 

Code blocks uses strongly typed programming approach. Therefore, the type of a parameter must be provided.

After that, the parameter can be used as a variable.

Return value from a function

Return values are the values that a function returns when it has completed.

The void return specifies that the function does not have a return value.

To return a value from a function, you need:

1. Open function properties and mark has return as true.

2. Replace the void with the type you need.

3. In the end of the function, pass the value you need to return.

If you put a tick near , you will have to return a collection.

Use seed instance

Seed instances represent a group of constants. You create a seed instance from the data type details page in Builder.

To use a seed instance in the Code Blocks, go to the INSTANCES section from the Data panel, select the instance you need and Fetch it.

In the example, we fetched a Deutch language (System seed instance).

You can fetch and use any seed instance you created in the same way.

I need to hide a field in the form.

In the code blocks, use Form block set show element and pass the element you need to hide.

You can also pass an element id to hide a button, for example.

FAQ

Create a Text variable?

In the Code blocks, to create a variable, start from clicking the “Make variable” button in the Data Panel on the right.

Then Select the Data Type you need. In our case this type is String, that is the Text in programming.

Give the name to a variable (press Enter and it will be auto-generated).

Now you have two options: create a single variable, or collection. Collection contains single variables. Click Create and you are done.

As you created a variable, you can see it in the Data Panel on the right.

How can I make queries to a database?

Create a Server function. Use stream block to obtain a collection from a Data Type, where to filter it by condition, group&aggregate to aggregate the data.

Here only lessons, equal to the current (item) will be used in the item.@Homework collection.

Read about Queries.

How to persist my changes to the database?

A variable may be created in the code blocks, but to save it in the Database, use create and update blocks.

From Block Panel -> SAVE use create block to save the vote and update to update the existing item.

Below we update a product and make it discontinued if available units num is  zero.

What is the difference between Client and Server actions?

Server blocks are translated into Java code and Client blocks are translated into Javascript code.

Server blocks require rebuild to come into action, and are used to query a database. Client blocks take effect after saving and are used, for example, to show an element or to create a web-link.

 

Do I need to publish my app every time I make changes?

If the Publish button is orange, you need a rebuild.

The application is not available during publishing.

Can I call an external API?

Yes, you can! Please,, follow the links below to see the functionality:

Code examples

Video example

How does action differ from a function?

Action is strongly related to an element on a page (onChange, onClick) or to a data type itself (onLoad, onSave). It uses the item key parameter that is responsible for a unique instance. You cannot call an action from type A in type B.

Meanwhile, functions are related to a type only at creation, you access it using this data type. You can  call it from type B using type A.

How to write code in Java or JS?

Native code blocks allow you to write the language’s code directly inside the block. In the server actions, native code is translated into Java code, while on client it is JS code.

In the example below we combine code blocks with a JavaScript code.

We use <%url%> syntax to pass a variable to a native code block.  

All the logic you can create using JS or Java can be written in the Native code blocks.

Note: only users  with a professional builder can use native code blocks.

Use external libraries

In Code Blocks click on the Imports button.

Enter the libraries you need.

To upload a .jar file of the library to your builder, use the Custom resources page from the Advanced⇒Resources submenu.