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 Form definition

1.2.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.2.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 String Uppercase
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
Copy
/**
 * 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;
}else{
    result.errmsg = "Article does not exist or does not correspond!";
}
return result;
Copy
/**
 * 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);
}else{
    form.set("codart",codart);
    form.set("nomart",nomart);
    form.set("stock",stock);
}

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 String Uppercase
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 Integer
No entry Yes
Description Stock
Required No



1.2.3 Nest form to menu

The created form is nested to a menu, which in this case is TEST_STOCK.

1.3 Execute the RF form

Starts the execution of the program RF2020 Terminal. In particular, verify that the elements corresponding to config and form have been correctly reported.

Copy
-config login_test
-menu TEST_STOCK

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



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



1.3.1 Query for a non-existing item

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

1.3.2 Query for an existing item

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