Both PHPRunner and ASPRunner.NET come with 'Prevent duplicate values' feature however this option will check the whole table for similar values. If we want to narrow it down and only check the records that belong to the same parent, we need to do this manually.
We will talk again about master-details relationship between Orders and OrderDetails and our goal is to prevent a product from being added more than once to the same order.
First, we need to proceed to OrderDetails Add page, open ProductID field properties and add a field event tied to change.
- ClientBefore event
Here we need to get the current value of ProductID field and pass it to Server part of the code along with the current OrderID.
params["value"] = this.getValue();
var ctrlOrderID = ctrl.getPeer('OrderID');
params["OrderID"] = ctrlOrderID.getValue();- Server event
Our job here is to check how many records with this ProductID are already added to this order. The result will be either 0 or 1 and we pass it to ClientAfter event.
$sql = DB::PrepareSQL("select count(*) from `order details` where OrderID=:1 and ProductID=:2",
$params["OrderID"], $params["value"] );
$count = DB::DBLookup($sql);
$result["count"] = $count;- ClientAfter event
Based on the count we either enable the Save button or disable it and show a message.
if (result["count"]==0) {
// enable Save button
pageObj.getItemButton('add_save').removeClass('disabled');} else {
// reset the control, disable Save button, show a message
ctrl.setValue("");
pageObj.getItemButton('add_save').addClass('disabled');
swal('This product is already a part of the order. Choose another one'); }