Skip to main content

7 - Validate Transaction Dates

Last updated 16/12/2025

This documentation will explain how we can use metadata provided in the Buy field to validate the purchase of shares to a specific date.

When in Five's Code Editor you will be interacting with Five's API. In the

CheckBuyQuantity()
function you will be using the following functions and properties:

- is a property on the
Five
object that returns the value associated to the combination of the action (form) ID and the field ID for the form in the stack.

- is a function on the
Five
object that returns an error with an optional message and notification parameters.

- is a function on the
Five
object that provides metadata from a field.

- is a function on the
Five
object that returns a indicating success.

Add the CheckBuyQuantity Function

1. Click Logic in the menu followed by Code Editor in the sub-menu.


Code Editor menu Item
Figure 1 - Code Editor menu item

2. Click the Add New Code Button.


Add New Code button
Figure 2 - Add New Code button

3. Type CheckBuyQuantity in the Function ID field.

4. Select TypeScript in the Language field and click the OKAY button.


Add CheckBuyQuantity function
Figure 3 - Add CheckBuyQuantity function

5. Click the Copy button on the code block below.

TypeScript
CheckBuyQuantity
function CheckBuyQuantity(five: Five, context: any, result: FiveError) : FiveError {
const qty: number = parseInt(five.field.Quantity);
if (qty > five.form.Sells.Quantity) {
return five.createError(result, 'Stock quantity exceeds quantity in this sale');
}

if (qty > five.getMetadata('Allocations', 'BuyKey', 'Holding')) {
return five.createError(result, 'Stock quantity exceeds remaining stock in this buy');
}

return five.success(result, '');
}

6. Paste the code block over the template in the Code Editor and click the Save Current Tab button.


Save Current Tab button
Figure 4 - Save Current Tab button

Attach the CheckBuyQuantity Function

The

CheckBuyQuantity()
function needs to be attached to the Quantity field on the Allocations form. The function can then use the metadata provided in the Buy field to know the quantity of stock for each purchase date.

Path: Allocations form > General page > Quantity field > On Validate event

1. Click Visual in the menu followed by Forms in the sub-menu.


Forms menu item
Figure 5 - Forms menu item

2. Select the Allocations record in the list and click the Pages tab.


Pages tab
Figure 6 - Pages tab

3. Select the General record.


General record
Figure 7 - General record

4. Click the Fields tab.


Fields tab
Figure 8 - Fields tab

5. Select the Quantity record.


Quantity field
Figure 9 - Quantity record

6. Click the Events tab.


Events tab
Figure 10 - Events tab

7. Either click the Edit button in the form app bar or click directly in the On Validate field.

8. Select CheckBuyQuantity in the On Validate field.


On Validate field
Figure 11 - On Validate field

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


Save button
Figure 12 - Save button

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


Save button
Figure 13 - Save button

tip
This is a good time to deploy/run the Portfolio application to see how the
CheckBuyQuantity()
function works!

Test the Sells Form

1. Select the Growth Portfolio record in the list and click the Down button in the form app bar.


Down button
Figure 14 - Down button

2. Select Sells in the menu and click the Add Item button.

3. Select TSLA in the Stock field and the current date in the Transaction Date field.

4. Type 11000 in the Quantity field, 400.00 in the Price field, 10.00 in the Fees field, and press tab.


Add TSLA sell record
Figure 15 - Add TSLA sell record

5. Click the Add Allocations button.


Add Allocations button
Figure 16 - Add Allocations button

6. Select 2025-11-30 in the Buy field, type 11000 in the Quantity field, and press tab.


Stock quantity notification
Figure 17 - Stock quantity notification

info
An error is returned letting us know that the Stock quantity exceeds remaining stock for this buy. This is because even though we have a total of 14000 TSLA shares, we only purchased 10000 TSLA shares on the transaction date 2025-11-30.

7. Click the OK button.


OK button
Figure 18 - OK button

8. Type 10000 in the Quantity field.

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


Save button
Figure 19 - Save button

Problem
If you take a look at the image below, we would be able to save the Sell record at this point as we have 10000 TSLA shares purchased on the 2025-11-30. If we were to click the Save button on the Sells form, the sale would be submitted even though the values in the Quantity fields for both forms do not equal. In Five, we can create a server-side function to ensure the values in the Quantity fields match on the Sells and Allocations forms to ensure the integrity of the data.

Unmatched Quantity fields
Figure 20 - Unmatched quantity fields

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


Cancel Button
Figure 21 - Cancel button

11. Close the browser tab and return to Five.