Using dm.recalculate() to apply changes to form values

I have an exchange_rate value stored in a super_user table. The user can adjust that value and then go to the stock_list form, where they should see prices in two currencies.

The exchange rate is used to calculate the value in the second currency: (other_currency_value = retail_price * exchange_rate).

However, changes made to the exchange_rate in the super_user form don’t get transfered through to the stock_list form, when I just switch between the two forms (from super_user to stock_list). They do appear in the form, if I restart Servoy Developer, but I need them to appear as soon as I switch to the stock_list form.

I am using an unstored calculation to work out the value in the other currency. The code for that calculation is as follows:

var other_currency_value = 0;
if (stock_to_superuser.exchange_rate > 0)
{
	other_currency_value = retail_price_inc_vat * stock_to_superuser.exchange_rate;
}

return other_currency_value;

I am also using a method in my stock_list form named recalculate_field(), which is run from the onShow slot. That method is used to recalculate the value in the other_currency_value column. The code from this method is as follows:

databaseManager.saveData();

if (foundset.getSize() > 0)
{
	databaseManager.recalculate(foundset);
}

I have also tried using other code in various ways to rectify this problem, but have had no joy with it.

I get the following error message when the stock form loads:

Exception executing calculation: calculated_stock_info of table stock, error: TypeError: Cannot convert null to an object. (recalculate_field; line 6)
java.lang.RuntimeException: Exception executing calculation: calculated_stock_info of table stock, error: TypeError: Cannot convert null to an object. (recalculate_field; line 6)
TypeError: Cannot convert null to an object. (recalculate_field; line 6)

Therefore can anyone out there tell me where I am going wrong with this approach. Any help would be greatly appreciated.

Craig

The error you are getting can probably be solved by:

var other_currency_value = 0; 

if (databaseManager.hasRecords(stock_to_superuser)) {
	if (stock_to_superuser.exchange_rate > 0) 
	{ 
	   other_currency_value = retail_price_inc_vat * stock_to_superuser.exchange_rate; 
	} 
	
	return other_currency_value;
}

As far as the calculation not carrying values through until you restart, does your “stock_to_superuser” relation have a global on the left side of its match rules?

Hi David,

I have tried using the additional condition that you suggested above, but it doesn’t solve the problem. I still get the same error.

Also, I do have a global relation from stock to superuser. The single condition for that relation is:

globals.g_my_company = owner

‘owner’ is an attribute in the superuser table.

As a bit of further info, I do get the correct values in the other_currency column in the stock_list form, if I move through more through a few other forms before going to the stock_list form. This is obviously starting from the superuser form, where I’ve adjusted the exchange rate.

As I’m sure you can understand, I really need to see the correct results when moving from superuser to stock_list in a single step. (Without going anywhere in between).[/quote]