getMetadata()
Last updated 16/09/2024
Example One
The following code uses the metadata in the InventoryLookup query attached to the InventoryKey form field. The function is attached to the On List Select event and when an inventory is selected the price is populated with the selected inventory's price using the metadata provided in the query. As the price is being generated from the current form, the
actionID
parameter is optional in the function.
The metadata is provided to the InventoryKey form field using the display type _LookupQuery. The
CategoryKey
parameter is used to only show the inventories for the selected category.
info
At a minimum, a _LookupQuery must have the primary key field and a value field to be shown in the lookup.
InventoryLookup query with the price metadata
SELECT
InventoryKey,
Product,
Price
FROM
Inventory
WHERE
CategoryKey = ?
getMetadata()
is used to get the metadata using the fieldID
and the metadataID
parameters. As it is on the
current form, the actionID
parameter is optional.
Getting the metadata on the current form to populate the price
function OnInventorySelect(five: Five, context: any, result: FiveError) : FiveError {
five.field.PricePerItem = five.getMetadata('InventoryKey', 'Price');
return five.success(result);
}
Getting the metadata on the current form to populate the price
function OnInventorySelect(five, context, result) {
five.field.PricePerItem = five.getMetadata('InventoryKey', 'Price');
return five.success(result);
}
Example Two
The following code uses the metadata in the CurrentStockHolding query attached to the StockKey form field. The function is attached to the On Validate event and validates the current holding so that the shares cannot be sold if they aren't held.
CurrentStockHolding query with metadata
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
getMetadata()
is used to get the metadata using the actionID
, fieldID
, and the metadataID
parameters.
Getting the metadata Holding from the StockKey field on the Sells form
function CheckStockQuantity(five: Five, context: any, result: FiveError) : FiveError {
const qty: number = parseInt(five.field.Quantity);
if (qty > five.getMetadata('Sells', 'StockKey', 'Holding')) {
return five.createError(result, 'Stock quantity exceeds current holding');
}
return five.success();
}
Getting the metadata Holding from the StockKey field on the Sells form
function CheckStockQuantity(five, context, result) {
const qty = parseInt(five.field.Quantity);
if (qty > five.getMetadata('Sells', 'StockKey', 'Holding')) {
return five.createError(result, 'Stock quantity exceeds current holding');
}
return five.success();
}