Error Deleting - Row not Found

I make form Master/Detail Records,
in master form, the ID field (PK) name order_id, and I use generated string to fill the field, I put the code on event beforeinsert

//make new number

/**

  • Check for an existing record
    */

// SQL statement parameters
$check_table = ‘tbjualm’; // Table name
$check_where = " fnojual = ‘condition’"; // Where clause
$incnojual=0;

// Check for record
$check_sql = 'SELECT fnojual ’
. ’ FROM ’ . $check_table
. ’ ORDER BY CONVERT(fnojual, SIGNED) DESC ';
sc_lookup(dataset, $check_sql);

if (isset({dataset[0][0]}))
{
// row found
// Record found
$incnojual = sprintf("%’.09d
",{dataset[0][0]} + 1);
}
else
{

}

{fnojual} = $incnojual;

So the order_id will automatically filled when I press Save, after that, I create records for Details, and save is no problem.
But when I try to delete detail record by pressing delete icon, it show error with : Error Deleting - Row Not Found

What seems to be the problem ?

please

thanks