← servoy magazine

[Tip] Retrieving a value from a non-related table

  • Tips

by Riccardo Albieri

While you're developing, you may need to assign to a field a value from a field residing on a non-related table.

There are several ways to accomplish this task (creating a dummy relationship, parking the value in a global etc.). If you prefer not to create ad hoc relationships or use a global, you can always count on the power of SQL.

The following example checks if the field 'testid' of table SALES has a value; if it doesn't, the method retrieves the first value of field testid of table TEST.

if ( !testid )
{

    //Get a dataset based on query
    var maxReturedRows = 1;        //useful to limit number of rows
    var query = 'select MIN(testid) from test';
    var dataset = databaseManager.getDataSetByQuery(controller.getServerName(), query, null, maxReturedRows);

    testid = dataset
}

Of course, if you want to assign the value of the last record, you just have to use MAX instead of MIN.