Calcutlations - outside of a form.

Questions, tips and tricks and techniques for scripting in Servoy

Calcutlations - outside of a form.

Postby dlclark » Wed Feb 09, 2022 9:55 pm

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
dlclark
 
Posts: 38
Joined: Sat Jan 08, 2022 5:39 pm

Re: Calcutlations - outside of a form.

Postby dlclark » Mon Feb 14, 2022 8:22 pm

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

}
dlclark
 
Posts: 38
Joined: Sat Jan 08, 2022 5:39 pm

Re: Calcutlations - outside of a form.

Postby steve1376656734 » Mon Feb 14, 2022 8:59 pm

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
Steve
SAN Developer
There are 10 types of people in the world - those that understand binary and those that don't
steve1376656734
 
Posts: 339
Joined: Fri Aug 16, 2013 2:38 pm
Location: Ashford, UK


Return to Methods

Who is online

Users browsing this forum: No registered users and 12 guests