Hi andemosa,
You can add a lookup query to the Sales form to have more data available from the Drug table and then use the getMetadata() function to calculate the total. I have provided the steps for you below.
I have created a similar setup to you and have a Drug and Sales table and a Drug and Sales form.
Add a Drug Lookup Query
This query will provide metadata to the Sales form through the DrugKey field.
-
Select Visual in the Menu.
-
Select Queries in the sub-menu.
-
Click the Add Item button.
-
Type Drug Lookup in the Title field.
-
Click in the Query field to open the Query Builder.
- Click the SQL tab.
- Type the following:
SELECT
DrugKey,
Name,
SellingPrice
FROM
Drug
- Click the Save button.
- Click the Fields tab. Note: You need to click the Fields tab to populate your query fields before saving the query record!
- Select the SellingPrice record.
-
Either click the Edit button or click directly in the Display Type field.
-
Click the lookup icon in the Display Type field and select _Float.2.
- Click the Save button in the form app bar.
- Click the Save button in the form app bar above the list.
Attach the Drug Lookup Query
Instead of the DrugKey field on the Sales form just being a straight lookup, we can edit this and make it have a display type of _LookupQuery and attach the Drug Lookup query to it, this will then contain the metadata (SellingPrice) on the DrugKey field.
-
Click Visual in the menu.
-
Click Forms in the sub-menu.
-
Select your Sales form record.
-
Click the Pages tab.
- Select the Page record holding the DrugKey field.
- Click the Fields tab.
- Select the DrugKey record.
-
Either click the Edit button in the form app bar or click directly in the Reference Form field.
-
Click the Cancel button in the Reference Form field.
-
Click the lookup icon in the Display Type field and select _LookupQuery.
-
Click the lookup icon in the Lookup Query field and select DrugLookup.
- Click the Save button in the form app bar.
- Click the Save button in the form app bar above the list.
Add the CalculateTotal Function
The CalculateTotal function will now have access to the SellingPrice field through the Drug Lookup query.
-
Select Logic in the menu.
-
Select Code Editor in the sub-menu.
- Click the Add New Code button.
-
Type CalculateTotal in the Function ID field.
-
Click the lookup icon in the Language field and select JavaScript.
-
Click the OKAY button.
-
Type the following in the body of the function:
five.field.Total = five.field.Quantity * five.getMetadata(‘Sales’, ‘DrugKey’, ‘SellingPrice’);
-
Click the Save Current Tab button.
Note: getMetadata(FormID.FieldID.QueryFieldID)
Please refer to the following documentation and examples to have a thorough understanding of the function getMetadata().
getMetadata() | Five | Low-Code For Real Developers
getMetadata() | Five | Low-Code For Real Developers
Attach the CalculateTotal Function to the On Enter Event
The Sales form will need to be edited again so that you can now attach the CalculateTotal function to the Total field on the Sales form.
-
Select Visual in the menu.
-
Select Forms in the sub-menu.
-
Select your Sales record in the list.
-
Click the Pages tab.
- Select the page record holding the Total field.
- Click the Fields tab.
- Select the Total record.
- Click the Events tab.
-
Either click the Edit button or click directly in the On Enter event.
-
Click the lookup icon in the On Enter field and select CalculateTotal.
- Click the Save button in the form app bar.
- Click the Save button in the form app bar above the list.
You can run your application now and once you have added a drug record like below.
You can then add a Sales record and the Total field will automatically calculate on entering the Total field.