Page 1 of 1

Calcutlations - outside of a form.

PostPosted: Wed Feb 09, 2022 9:55 pm
by dlclark
Working on our existing code base, which has functions declared which compute for example outstanding invoice total due.

Currently the code reads (code was written probably 10 or more years ago and I am migrating it to NGClient and the latest Servoy code base (down from about 5,000 warnings to about 100 now) learning much in the process!

function c_0_30_total_due() {
var vTotal = 0.0;
var relatedFoundCount = databaseManager.getFoundsetCount( advertiser_invoices_self_adv_id));

.....

the related found count code - yields an error: undeclared variable databaseManager - yet when the function is called, the databaseManager is faulted in somehow.

Question: how should the foundset for a CALCULATION be accessed?

Thanks,

Daryl

Re: Calcutlations - outside of a form.

PostPosted: Mon Feb 14, 2022 8:22 pm
by dlclark
Looks like the documentation on calculations and relationships gives a hint.
function c_0_30_total_due()
{
var vTotal = 0
var recordCount = advertiser_invoices_self_adv_id.getSize();

for (var i = 1; i <= recordCount; i++)
{
var vRecord = advertiser_invoices_self_adv_id.getRecord(i)
vTotal = vTotal + vRecord.c_0_30
}
return vTotal

}

Re: Calcutlations - outside of a form.

PostPosted: Mon Feb 14, 2022 8:59 pm
by steve1376656734
Hi Daryl,

I think your second post may not give you the result you want. Using advertiser_invoices_self_adv_id.getSize(); will only give you the number of records currently loaded in the foundset (default 200) and will therefore only calculate the total due for those records. databaseManager.getFoundsetCount() returns the total number of records in the foundset by executing a select count(*) from <table_name>.

You may be better off using an aggregate on the table to calculate the sum of the column that you are interested in.

Alternatively, you could put the current code from the calculation into a scope method and call this from the calculation. This will have the benefit of being identical to the current process and also remove the warning regarding the call to databaseManager (which is not allowed from a calculation), but in reality has the same drawbacks as calling databaseManager directly in the calculation and ignoring the warning!

HTH
Steve