Tips & Tricks - Restrict data input to one record only
A safe and elegant solution to restrict the number of data records that can be inputted to 1 is the conditional display of buttons. With this method, a condition is specified, is performed on the server side, and depending on the result of the check, the button will be displayed or hidden. So that you can configure the conditional display of buttons, you need to have activated the expert options.
Hide a button if there is already one data record
On a page of your choice, create a new button. Open the properties dialog. On the “General” tab, activate the setting Display if condition met under Options.
Click on "Edit condition" and enter the following script in the Velocity editor:
#set($show_buttoncontrol3A451A26 = false)
## Get number of records
#set($l_statement = $PreparedQuery.prepare($DbConnection, "SELECT COUNT(*) FROM XDATAGROUP00D1A2185"))
#set($l_recordCount = $l_statement.executeAndGetScalarValue(0))
$l_statement.close()
#if($l_recordCount == 0)
#set($show_buttoncontrol3A451A26 = true)
#end
The script executes an SQL statement that checks how many data records are stored in the data group. If there are no data records, then the button will be displayed. In the script, replace the name of the button (buttoncontrol3A451A26) with the name of your button. You can find the name in the editor in the application structure. Replace the name of the data group (XDATAGROUP00D1A2185) with the name of your data group. Close the editor by clicking on "OK". Close the properties dialog of the button by clicking on "OK". Save the application. If you open the application in the browser, the button will be hidden as soon as a data record is saved in the corresponding application data group.
Hide a button if there is already one data record from the current user
With this example, each user may create one data record only. Follow the same steps as in the first example But insert the following script instead of the script above:
#set($show_buttoncontrol3A451A26 = false)
## Get number of records
#set( $l_statement = $PreparedQuery.prepare($DbConnection, "SELECT COUNT(*) FROM XDATAGROUP00D1A2185 WHERE LUSERIDINSERT = ?"))
$l_statement.setInt(1, $User.getId())
#set( $l_recordCount = $l_statement.executeAndGetScalarValue(0))
$l_statement.close()
#if($l_recordCount == 0)
#set($show_buttoncontrol3A451A26 = true)
#end
Replace the name of the button and data group with the corresponding values from your application.