This case details how to create a simple query to the database based on the information entered by the user.

The example consists of querying a table where stock information is stored, and when the item code is entered by the user, its name and the stock is returned.

1 Get the stock of an item

1.1 Analisis

1.1.1 Process flow

The flow designed for this example is as follows:

1.1.2 Low-level mockup

This is the low level mockup of the form

1.2 Data for example

The first step of the example is to create new test tables with consistent data in your own database.

CREATE TABLE devtest_garticul (
    codigo  char(16)        not null,
    nomart  varchar(100)    not null

ALTER TABLE devtest_garticul LOCK MODE (ROW);
INSERT INTO devtest_garticul VALUES ('1000001', 'Mechanical keyboards');
INSERT INTO devtest_garticul VALUES ('1000002', 'Ergonomic mouse');
INSERT INTO devtest_garticul VALUES ('1000003', 'Panoramic monitors');

CREATE TABLE devtest_galmstku (
    codart  char(16)        not null,
    codubi  char(5)         not null,
    stkact  integer         not null	default 0

ALTER TABLE devtest_galmstku LOCK MODE (ROW);
INSERT INTO devtest_galmstku VALUES ('1000001', '10001', 10);
INSERT INTO devtest_galmstku VALUES ('1000002', '10001', 20);
INSERT INTO devtest_galmstku VALUES ('1000002', '10002', 15);
INSERT INTO devtest_galmstku VALUES ('1000003', '10002',  5);


Following the definition of the Program execution, the tables of the example must be compiled in the database corresponding to "-dbdata".


If you use the test tables listed here, or other similar ones, do not forget to delete it at the end of the tests.

DROP TABLE IF EXISTS devtest_garticul;
DROP TABLE IF EXISTS devtest_galmstku;

1.3 Form definition

1.3.1 RF Form

To define the RF form header, it is sufficient to define the obligatory form_name field. Although, for this example the form_desc and form_comment fields are also filled in.

Field name Content Example value
form_name Short name of the form devtest_getstk
form_desc Description of the form Get stock
form_comment Long comment about the form With this form the user can check the stock of a specific item.

For this example it is not necessary to define any code in the "JS Start code" section or in "Form Server Rollback".

1.3.2 Form fields

For this example, once the RF form is defined, the following fields are created:

RF Field name Description
codart Item code informed by the user.
nomart Name of the item that has been informed.
stock Stock of the item that has been informed.

codart field

These are data entered in the form fields section corresponding to codart.

Field name Example value
Order 2
Group 0
Name codart
Type S
No entry No
Description Cod. Art.
Required Yes

This form field has the following logic:

Code Logic description
Field After Server Code The item data is obtained. If it exists, it sets its name in the "nomart" property and stores the item's stock in the "stkact" property, both belonging to the "result" object. This in order to return both values to the form. In case the item does not exist, it returns an error message in the variable "errmsg".
Field After Client Code Checks if there is any message in the variable "errmsg", if there is, it displays the message and positions itself in the same field (preventing advancing until a correct value is entered), otherwise, the RF Terminal shows the obtained values for each field.
Field Before Server Code N/A
Field Before Client Code N/A
 * The "result" object is defined, which return to the RF form the values of 
 * the codart, nomart and stock fields, as well as the error message field: errmsg.
var result = {
    codart : "",
    nomart : "",
    stock  : 0,
    errmsg : ""

 * The value entered by the user for the "codart" field is obtained with the 
 * 'fields' object, and stored in a variable.
var mStrCodart = fields.codart;

 * The sum of the item's stock is obtained and stored in the variable mIntStock.
var mIntStock = Ax.db.executeGet(`
        SELECT SUM(stkact)
          FROM devtest_galmstku
         WHERE devtest_galmstku.codart  = ?
    `, mStrCodart);

 * The name of the item is obtained and stored in the object mStrArt.
var mStrArt = Ax.db.executeQuery(`
        SELECT codigo, nomart
          FROM devtest_garticul
         WHERE codigo = ?
    `, mStrCodart).toOne();

 * In case the item exists, returns codart, nomart and stock in the "result" object.
 * Otherwise, it returns an error message (errmsg) in the "result" object.
if (mStrArt.codigo !== null) {
    result.codart = mStrArt.codigo;
    result.nomart = mStrArt.nomart;
    result.stock  = mIntStock;
    result.errmsg = "Article does not exist or does not correspond!";
return result;
 * If there is an error message, it returns it, while positioning itself in 
 * the same field.
 * Otherwise, it returns the item code, name and stock.
if (errmsg != "") {
    form.setFocus("codart", errmsg);

It is not necessary to use 'form.set' to assign the value to the form fields, since the properties of the object returned in the Field After Server Code statement have the same name as the variable and therefore adopt those values directly.

nomart field

This RF field is descriptive only, so it has no execution code.

The values defined for this RF field are:

Field name Example value
Order 4
Group 0
Name nomart
Type S
No entry Yes
Description Nom. Art.
Required No

stock field

This RF field is also descriptive, so it has no execution code.

The values defined for this RF field are:

Field name Example value
Order 6
Group 0
Name stock
Type I
No entry Yes
Description Stock
Required No

1.4 Execute the RF form

Access the devtest_getstk form through the corresponding menu in the RF Terminal:

As programmed, the system requires the user to report the item code:

1.4.1 Consult for a non-existent item

If the requested article code does not exist, a controlled error is displayed on the RF Terminal screen.

1.4.2 Consult for an existent item

If the consulted item code exists, the item name and stock information is displayed on the RF Terminal screen.