Skip to main content

7 - Validate Transaction Dates

Last updated 3/04/2023

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

Add the CheckBuyQuantity Function

1. Select Logic in the menu.

2. Select Code Editor in the menu.


Code Editor Menu Item
Figure 1 - Code Editor menu item


3. Click the Add New Code Button.


Add New Code Button
Figure 2 - Add New Code button


4. Type CheckBuyQuantity in the Function ID field.

5. Click the lookup icon in the Language field and select TypeScript.

6. Click the OKAY button


Add the CheckBuyQuantity Function
Figure 3 - Add the CheckBuyQuantity function


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


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, '');
}



8. Paste the code block over the template in the Code Editor.

9. Click the Save Current Tab button.


Save the CheckBuyQuantity Function
Figure 4 - Save the CheckBuyQuantity function


Attach the CheckBuyQuantity Function

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

1. Select Visual in the menu.

2. Select Forms in the sub-menu.


Forms Menu Item
Figure 5 - Forms menu item


3. Select the Allocations record in the list.

4. Click the Pages tab.


Pages Tab
Figure 6 - Pages tab


5. Select the General record.


General Record
Figure 7 - General record


6. Click the Fields tab.


Fields Tab
Figure 8 - Fields tab


7. Select the Quantity record.


Quantity Field
Figure 9 - Quantity field


8. Click the Events tab.


Events Tab
Figure 10 - Events tab


9. Click the lookup icon in the On Validate field and select CheckBuyQuantity.


Attach the CheckBuyQuantity Function
Figure 11 - Attach the CheckBuyQuantity function


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


Save the CheckBuyQuantity Event
Figure 12 - Save the CheckBuyQuantity event


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


Save the Allocations Form
Figure 13 - Save the Allocations form


Run the Portfolio Application

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


Run Button
Figure 14 - Run button


2. Select the Growth Portfolio record.

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


Down Button
Figure 15 - Down button


Test the CheckBuyQuantity Function

1. Select Sells in the menu.

2. Click the Add Item button.

3. Click the lookup icon in the Stock field and select AEI.

4. Click the calendar icon in the Transaction Date field, select the current date, and click the OK button.

5. Type 1100 in the Quantity field.

6. Type 1.00 in the Price field.

7. Type 10.00 in the Fees field, press tab.


Add an AEI Sells Record
Figure 16 - Add an AEI sells record


8. Click the Add Allocations button.


Add Allocations Button
Figure 17 - Add Allocations button


9. Click the lookup icon in the Buy field and select your yesterday date.

10. Type 1100 in the Quantity field.

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


Add an Allocations Record
Figure 18 - Add an Allocations record


info
An error is returned letting as know that the Stock quantity exceeds remaining stock in this buy. This is because even though we have a total of 1120 AEI shares, we only purchased 1000 AEI shares on the transaction date 2023-03-27.


12. Click the OK button.


Stock Exceeds Quantity Remaining Error
Figure 19 - Stock exceeds quantity remaining error


13. Type 1000 in the Quantity field.

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


Validates Shares to Transaction Dates
Figure 20 - Validates shares to transaction dates



Problem
If you take a look at the image below, we are able to save at this point as we have 1000 AEI shares purchased on the 2023-03-27. If we were to click the Save button on the Sells form, the sale would be submitted even though the Quantity fields on the Sells and Allocations forms do not match. In Five, we can create a backend function to ensure the values in the Quantity fields match on the Sells and Allocations form.



Unmatched Quantity Fields
Figure 21 - Unmatched Quantity fields


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


Cancel Button
Figure 22 - Cancel button


16. Close the browser tab and return to Five.