Tips & Tricks - Limiting Input to a Single Record
The conditional display of buttons offers a secure and elegant solution for limiting data entry to a single record. Here, you can define a validation condition that is executed on the server side and that shows or hides the button depending on the result of the validation. To configure the conditional display of the button, you should first enable the expert options.
Hiding the button when a record exists
Create a button on any application page. Open the Properties dialog box. On the "General" tab, under Options, enable the " Conditional Display" setting.
Click "Edit Condition" under "
" 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 records are in the data group. If no record exists, the button is displayed. In the script, replace the button name (buttoncontrol3A451A26) with the name of your button. You can find this name in the editor under " Application Structure." Also replace the name of the data group (XDATAGROUP00D1A2185) with the name of your data group. Close the editor by clicking "OK." Close the button's Properties dialog box by clicking "OK." Save the application. If you open the application in your browser now, the button will be hidden as soon as a record is entered in the corresponding data group within the application.
User-Dependent Hiding of the Button When a Record Exists
With this example, each user can create exactly one record. To do this, follow the same steps as in the first example. Instead of the script from the first example, insert the following script:
#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
Here, too, replace the names of the button and data group with the names of your button and data group.