Populate Detail form from calculated value on Master form

Populate fields in new records in the detailed form based on a global value from the master form.

I have a master/detail form made for a simple double-entry accounting application:

The records in the detail form are summarized in a “PHP method” in the Detail form and updated in the Master form, every time I add, change or delete a record in the detail form.

This code works like a charm.

The result from the “PHP method” also needs to be populated in two fields in the next record in the detail form.

This only works when clicking on the add new record button in the detail form.

But when the detail form automatically inserts the next row the new fields will be populated (based on an event code, see below) with the previous result of the “PHP method” and not the preferred new result of the “PHP method”

I would really appreciate some help on this issue. :pray: :slightly_smiling_face:

Application details etc.:

I am using Scriptcase 9.5.003 on an Ubuntu 18.04.5 LTS and PostgreSQL 13.2.

The two forms are linked with the Master/Detail Form name: Journal.

Master form:
financial_transactions (single record)
Fields:
financial_transaction_id, description, balance_chk
Events:
onNavigate and on_loade: sc_set_global({balance_chk});
AjaxEvents:
balance_chk_onChange:
sc_set_global({balance_chk});

Detail form:
financial_journals (multible records)
Fields:
journal_id (PK), financial_transaction_id (FK), account_id, amount, is_credit_v
PHP Metods name:
update_master
Code:

sc_lookup(rs, “SELECT SUM(amount * is_credit_v) from financial_journals WHERE financial_transaction_id={financial_transaction_id}”);

if(!isset({rs[0][0]}) || empty({rs[0][0]}))
{
{rs[0][0]} = 0;
}

sc_exec_sql(“UPDATE financial_transactions SET balance_chk = " . {rs[0][0]} . " WHERE financial_transaction_id = ‘{financial_transaction_id}’”);

$total = number_format({rs[0][0]}, 2, “.”, “,”);

sc_master_value(‘balance_chk’, $total);

Events:
onBeforeInsert, onAfterInsert, onAfterUpdate, onAfterDelete:
Code: update_master();

OnLoadRecord:
Code:

if ([balance_chk] < 0){
$is_credit_v_update = (1);
}
if ([balance_chk] > 0) {
$is_credit_v_update = (-1);
}
if ([balance_chk] == 0) {
$is_credit_v_update = (1);
}

if (empty({account_id}))
{
{is_credit_v} = $is_credit_v_update;
}

if ([balance_chk] < 0){
$balance = ([balance_chk] * -1);
}
if ([balance_chk] > 0) {
$balance = ([balance_chk]);
}
if ([balance_chk] == 0) {
$balance = (0);
}

if (empty({account_id}))
{
{amount} = $balance;

I have added a small screen recording (mp4) to illustrate the issue.

}