SOS: Help Me Please! - SC Executing onValidateSuccess Twice?

I have a form app that subtracts a value from a record in another table like this:

sc_exec_sql("UPDATE current_stock SET Amount = Amount - {Amount} WHERE ".$update_where);

​I noticed sometimes (totally random), the value is taken twice. So to troubleshoot, I added code to set up and initialize a counter on ScriptInit and increment it by 1 within onValidateSuccess and log this. That means, after the line above, I have

[norecs] += 1;

Then I log the product id (Prod), Warehouse id (WH), value being subtracted and [norecs]. Please see the picture below.

Note at 14:30:33, this log was inserted, followed by SC’s standard log entry (which was logged twice, still don’t know why as per my previous issue at http://www.scriptcase.net/forum/foru…-sc_log​)

Weird thing is 14secs later, it re-executes my troubleshooting logging code (which is highlighted in yellow.) Notice the [norecs] is still showing 1 which means it did not increment the [norecs].
But it subtracted 80 twice from the current_stock table!

Notice at 14:31:33 and 14:31:57, it works properly (i.e. troubleshooting log entry only once, subtracted only once though sc log entry is still twice).

The only safe way I’m thinking might be to do these manipulations as a stored procedure but I find them quite cumbersome so I’m hoping someone can give me other pointers before I explore that.

Thanks in advance.

PS. the subsection of onValidateSuccess looks like this:

<quote>

sc_exec_sql("UPDATE current_stock SET Amount = Amount - {Amount} WHERE ".$update_where);
[norecs] += 1;

$ip = $_SERVER[‘REMOTE_ADDR’];
$dt = date(‘Y-m-d H:i:s’);
sc_exec_sql(“INSERT INTO sc_log (inserted_date, username, application, creator, ip_user, action, description) VALUES (’$dt’,’[usr_login]’,‘form_app’,‘user’,’$ip’,‘insert’,‘Prod {ProductID}/WH {WarehouseID}/ Subtracted {Amount} for the [norecs] st/nd time’)”);

<unquote> [ATTACH=CONFIG]n69555[/ATTACH]

image_3029.png

picture didn’t seem to appear. [TABLE=“border: 0, cellspacing: 0”]
[TR]
[TD=“width: 135”]2016-06-22 14:30:33[/TD]
[TD=“width: 900”]Prod 32 /WH 2 / Subtracted 80 for the 1 st/nd time[/TD]
[/TR]
[TR]
[TD]2016-06-22 14:30:33[/TD]
[TD]–> keys <-- issuestockrecordid : 1534||–> fields <-- Date (new) : 2016-06-21||ProductID (new) : 32||Amount (new) : 80||WarehouseID (new) : 2[/TD]
[/TR]
[TR]
[TD]2016-06-22 14:30:33[/TD]
[TD]–> keys <-- issuestockrecordid : 1534||–> fields <-- Date (new) : 2016-06-21||ProductID (new) : 32||Amount (new) : 80||WarehouseID (new) : 2[/TD]
[/TR]
[TR]
[TD=“bgcolor: #FFFF99”]2016-06-22 14:30:47[/TD]
[TD=“bgcolor: #FFFF99”]Prod 32 /WH 2 / Subtracted 80 for the 1 st/nd time[/TD]
[/TR]
[TR]
[TD]2016-06-22 14:31:33[/TD]
[TD]Prod 11 /WH 2 / Subtracted 384 for the 1 st/nd time[/TD]
[/TR]
[TR]
[TD]2016-06-22 14:31:33[/TD]
[TD]–> keys <-- issuestockrecordid : 1535||–> fields <-- Date (new) : 2016-06-21||ProductID (new) : 11||Amount (new) : 384||WarehouseID (new) : 2[/TD]
[/TR]
[TR]
[TD]2016-06-22 14:31:33[/TD]
[TD]–> keys <-- issuestockrecordid : 1535||–> fields <-- Date (new) : 2016-06-21||ProductID (new) : 11||Amount (new) : 384||WarehouseID (new) : 2[/TD]
[/TR]
[TR]
[TD]2016-06-22 14:31:57[/TD]
[TD]Prod 13 /WH 2 / Subtracted 96 for the 1 st/nd time[/TD]
[/TR]
[TR]
[TD]2016-06-22 14:31:57[/TD]
[TD]–> keys <-- issuestockrecordid : 1536||–> fields <-- Date (new) : 2016-06-21||ProductID (new) : 13||Amount (new) : 96||WarehouseID (new) : 2[/TD]
[/TR]
[TR]
[TD]2016-06-22 14:31:57[/TD]
[TD]–> keys <-- issuestockrecordid : 1536||–> fields <-- Date (new) : 2016-06-21||ProductID (new) : 13||Amount (new) : 96||WarehouseID (new) : 2[/TD]
[/TR]
[/TABLE]

Personally I would try to do this in the onafterinsert/update. The onvalidate is intended for validations, not for processing. It could be that you mess with the commit chain, although that’s a wild guess to be honest.

Hi Albert, thanks. The code was actually in onValidateSuccess not onValidate but now I’ve shifted to onBeforeInsert and will watch. I do not want the records inserted in the form’s table if they do not get inserted into the other table successfully first.​