Skip to main content

4 - Use Metadata in a Query

Last updated 3/04/2023

This documentation is to demonstrate how you can create a query with metadata and use this query on a field with the display type _Lookup Query to filter the results you require. This same query can also have a menu item created and be used to show a report of the results.

When you ran the Portfolio application you saw that we were able to select a stock that we do not own to sell. We will write a query that contains metadata and attach it to the Stock field on the Sells form to only show the stocks we own.

In its most minimal form a _LookupQuery display type must have:

  • A primary key field to have uniqueness
  • A value field to display in the lookup list

1. Select Visual in the menu.

2. Select Queries in the sub-menu.


Queries Menu Item
Figure 1 - Queries menu item


Add the Current Stock Holding Query

1. Click the Add Item button.

2. Type Current Stock Holding in the Title field.

3. Click in the Query field to open Five's Query Builder.


Add the Current Stock Holding Query
Figure 2 - Add the Current Stock Holding query


4. Click the SQL tab.


SQL Tab
Figure 3 - SQL tab


5. Click the Copy button for the syntax below.


MySQL
Current Stock Holding
SELECT
StockKey,
StockCode,
Name,
SUM(Bought - Sold) AS Holding,
SUM(OriginalCost * (Bought - Sold) / Bought) AS Investment
FROM (
SELECT
Stock.StockKey AS StockKey,
Stock.StockCode AS StockCode,
Stock.Name AS Name,
Buy.Quantity AS Bought,
Buy.PortfolioKey AS PortfolioKey,
IFNULL((SELECT SUM(Allocation.Quantity) FROM ALLOCATION WHERE Allocation.BuyKey = Buy.BuyKey),0) AS Sold,
Buy.Total AS OriginalCost
FROM
Buy INNER JOIN Stock ON Buy.StockKey = Stock.StockKey
HAVING
(PortfolioKey = ?) AND
(Bought > Sold)
) AS Summary
GROUP BY
StockKey,
StockCode,
Name



6. Paste the syntax in the SQL Editor.


info
If you look at the SELECT statement you can see we are using the StockKey and the StockCode fields for the StockCode to be displayed in the Stock field's lookup list.

The SUM(Bought - Sold) will let us know how much of each stock we are currently holding.

The HAVING clause is used to see if we have more bought than sold for each stock to know whether to display the StockCode in the lookup list.


7. Click the Save button in the SQL Editor app bar.


Add the Current Stock Holding Syntax
Figure 4 - Add the Current Stock Holding syntax


8. Click the Fields tab.


Fields Tab
Figure 5 - Fields tab


9. Select the StockKey record.


StockKey Record
Figure 6 - StockKey record


10. Click in the Show If field and type false.


info
The StockKey field is a key field and holds a GUID so we do not want to show this value in our report.


11. Click the Save button in the form app bar.


Edit the StockKey Record
Figure 7 - Edit the StockKey record


12. Select the Holding record.


Holding Record
Figure 8 - Holding record


13. Click the lookup icon in the Display Type field and select _Integer.

14. Click the Save button in the form app bar.


Edit the Holding Record
Figure 9 - Edit the Holding record


15. Select the Investment record.


Investment Record
Figure 10 - Investment record


16. Click the lookup icon in the Display Type field and select _Currency.

17. Click the Save button in the form app bar.


Edit the Investment Record
Figure 11 - Edit the Investment record


18. Click the Parameters tab.


Parameters Tab
Figure 12 - Parameters tab


19. Click the Add Parameters button.


Add Parameters Button
Figure 13 - Add Parameters button


20. Type PortfolioKey in the Parameter ID field.

21. Type {{five.stack.Portfolio.PortfolioKey}} in the Parameter field.


Add the PortfolioKey Parameter
Figure 14 - Add the PortfolioKey parameter



info
Using Five's API we can get the data from the Portfolio table stacked above us using the property:

five.stack



22. Click the Save button in the form app bar.


Save the PortfolioKey Parameter
Figure 15 - Save the PortfolioKey parameter


23. Click the Save button in the form app bar above the list.


Save the Current Stock Holding Query
Figure 16 - Save the Current Stock Holding query


Working with Parameters

If you want to find transactions for just one Portfolio record, you can by setting a parameter type of Constant, this will only pull the data for that particular portfolio.


Constant Example
WHERE PortfolioKey = โ€˜6b087bf8-110d-4e9f-aff0-59d99858c437โ€™


To use the query repeatedly to get data from different Portfolio records, you can parameterize the query with an Expression and use the five.stack property. This will make it that when you are under a selected record you will have the data for that specific record as it is not hard-coded.


Expression Example
WHERE PortfolioKey = {{five.stack.Portfolio.PortfolioKey}}


Attach the Query to the Stock Field on the Sells Form

Path: Sells form > General page > Stock field

1. Select Visual in the menu.

2. Select Forms in the sub-menu.


Forms Menu Item
Figure 17 - Forms menu item


3. Select the Sells record in the list.

4. Click the Pages tab.


Pages Tab
Figure 18 - Pages tab


5. Select the General record.


General Record
Figure 19 - General record


6. Click the Fields tab.


Fields Tab
Figure 20 - Fields tab


7. Select the Stock record.


Stock Record
Figure 21 - Stock record


8.Click the lookup icon in the Display Type field and select _LookupQuery.

9. Click the Cancel button in the Reference Form field.

10. Click the lookup icon in the Lookup Query field and select CurrentStockHolding.


Attach the Current Stock Holding Query
Figure 22 - Attach the CurrentStockHolding query


11. Click the Save button in the form app bar.


Save the Stock Field
Figure 23 - Save the Stock field


12. Click the Save button in the form app bar above the list.


Save the Sells Form
Figure 24 - Save the Sells form


Create a Menu Item for the Current Stock Holding Query


info
The Current Stock Holding query can also have a menu item created so a user can see the results in a report style.


1. Select Visual in the menu.

2. Select Menus in the sub-menu.


Menus Menu Item
Figure 25 - Menus menu item


3. Click the Add Item button.

4. Type Current Stock Holding in the Caption field.

5. Type 2 in the Menu Order field.

6. Click the lookup icon in the Action field and select CurrentStockHolding (Query).

7. Click the lookup icon in the Parent Menu field and select Portfolios (Portfolios).


Optional
You can add an icon for the Current Stock Holding menu item by clicking the Edit button in the Icon field and selecting your image file.


Add the Current Stock Holding Menu Item
Figure 26 - Add the Current Stock Holding menu item


8. Click the Save button in the form app bar.


Save the Current Stock Holding Menu Item
Figure 27 - Save the Current Stock Holding menu item


Edit the Buys Menu Item


info
The Buys and Sells menu items were created by the Form Wizard, these need to be edited to re-order them into the positions we want.


1. Select the Buys record in the list.

2. Click in the Menu Order field and type 3.


Edit the Buys Menu Item
Figure 28 - Edit the Buys menu item


3. Click the Save button in the form app bar.


Save the Buys Menu Item
Figure 29 - Save the Buys menu item


Edit the Sells Menu Item

1. Select the Sells record in the list.

2. Click in the Menu Order field and type 4.


Edit the Sells Menu Item
Figure 30 - Edit the Sells menu item


3. Click the Save button in the form app bar.


Save the Sells Menu Item
Figure 31 - Save the Sells menu item


Run the Portfolio Application

This documentation is to demonstrate how the Current Stock Holding query works in the Portfolio application.

1. Click the Run button in Five's toolbar.


Run Button
Figure 32 - Run button


2. Select the Growth Portfolio record.

3. Click the Down button in the form app bar.


Down Button
Figure 33 - Down button


Test the Current Stock Holding Query

The first thing we now see in the child menu is the Current Stock Holding report and it shows us the stock we own with our share holding and what we have invested. We can see we own shares in the stocks BOQ, AMP, AEI, and BHP. The report shows us that we own 300 BOQ shares.


Current Stock Holding Report
Figure 34 - Current Stock Holding Report


Test the Sells Form

1. Select Sells in the menu.

2. Click the Add Item button.

3. Click the lookup icon in the Stock field.


Filtered Stock Field
Figure 35 - Filtered Stock field


info
We now only have the stocks we own in the Stock lookup list!


4. Select BOQ in the Stock lookup list.

5. Click the calendar icon in the Transaction Date field and select the current date.

6. Type 400 in the Quantity field, press tab.


Problem
The Current Stock Holding report showed us that we only own 300 shares in the BOQ stock, yet we are able to enter 400 in the Quantity field. In Five, we can fix this by creating a validation and attaching it to the Quantity field on the Sells form.



No Quantity Validation
Figure 36 - No quantity validation


7. Click the Cancel button in the form app bar.


Cancel Button
Figure 37 - Cancel button


8. Close the browser tab and return to Five.