Skip to main content

getMetadata()

Last updated 30/03/2023

Example One

The example below uses the metadata provided in the CurrentStockHolding query.

MySQL
CurrentStockHolding
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


The function first parses the Quantity field on the Five object as a number and assigns it to a variable called qty. Then, it checks if qty is greater than the current stock holding on the Five object,which is obtained using the getMetadata method with the parameters Sells, StockKey, and Holding. If qty is greater than the current stock holding, the function creates an error message using the createError method on the Five object and returns it as the result. Otherwise, the function returns success.

Get the metadata from the Sells, StockKey, and Holding query fields
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();
}

Example Two

The example below uses the metadata provided in the InventoryLookupQuery query.

MySQL
InventoryLookupQuery
SELECT
Inventory.InventoryKey,
Inventory.Product AS Product,
Inventory.Price AS Price,
Inventory.GSTExempt AS GSTExempt
FROM
Inventory
WHERE
Inventory.CategoryKey = ?

The function, retrieves the Price metadata value on the Items form on the Five object using the getMetaData method. It then sets the PricePerItem field on the Five object to the retrieved Price value. Finally, the function returns success.

Get the metadata from the Items, InventoryKey, and Price query fields
function PrefillPrice(five: Five, context: any, result: FiveError) : FiveError {
const Price = five.getMetadata('Items', 'InventoryKey', 'Price');
five.field.PricePerItem = Price;

return five.success();
}

Example Three

The example below uses the metadata provided in the InventoryLookupQuery query.

MySQL
InventoryLookupQuery
SELECT
Inventory.InventoryKey,
Inventory.Product AS Product,
Inventory.Price AS Price,
Inventory.GSTExempt AS GSTExempt
FROM
Inventory
WHERE
Inventory.CategoryKey = ?

The function, retrieves the Price and GSTExempt status of an item on the Five object's metadata using the getMetaData method. It also retrieves the quantity of the item on the Five object's Quantity field associated to the form. It then calculates the subtotal of the item by multiplying the Price and Quantity fields. If the item is not GST exempt, it calculates the GST by multiplying the Subtotal field by 0.1 (10%). Finally, it sets the GST and Total fields on the form using the field object attached to the Five object to the calculated values and returns success.

Get the metadata from the Items, InventoryKey, and GSTExempt query fields
function CalculateItemGstAndTotal(five: Five, context: any, result: FiveError) : FiveError {
const Price = Number(five.getMetadata('Items', 'InventoryKey', 'Price'));
const GSTExempt = Number(five.getMetadata('Items', 'InventoryKey', 'GSTExempt'));
const Quantity = five.field.Quantity;

const SubTotal = Price * Quantity;
let GST = 0;

if (GSTExempt === 0) {
GST = SubTotal * 0.1;
}

five.field.GST = GST;
five.field.Total = SubTotal + GST;
return five.success();
}