Skip to main content

8 - Add a Server-Side Function

Last updated 16/12/2025

This documentation will explain how to add a server-side function. To date we have only been using functions that execute on the client.

There are two types of events in Five:

  • On Event - executes on the client
  • Do Event - executes on the server

When we ran the Portfolio application, we saw that we could save a Sell record without the Quantity fields having equal values. We will add a function and attach it to the Do Complete event on the Sells form. The function will execute when the Save button is clicked on the Sells form and will verify that the single or multiple allocation records equal the quantity value on the Sells form before committing to the database.

This functionality can be performed on the client-side using the On Complete event, however, it would require extra code as Five assembles the data from all the forms into a single transaction array, this simplifies your code as you can iterate over all forms in one array. Essentially, when the Save button is clicked it sends the current data on the forms to the backend, and the backend will call the function with the transactions being saved.

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

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

Transactions
- is an array of transactions sent to the server containing all the records that the user either wants to create, update, or delete. Each transaction will contain an
actionID
for the action it is manipulating.

- 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 returns a indicating success.

Add the CheckValidSell 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 CheckValidSell in the Function ID field.

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


Add CheckVaildSell function
Figure 3 - Add CheckValidSell function

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

TypeScript
CheckValidSell
function CheckValidSell(five: Five, context: any, result: FiveError) : FiveError {

if (!context.Transactions) {
return five.success(result);
}

let soldQuantity: number = 0;
let totalAllocated: number = 0;

for (let i = 0; i < context.Transactions.length; i++) {
const record = context.Transactions[i];
if (record.ActionID === 'Sells') {
soldQuantity = Number(record.Values.Quantity);
} else if (record.ActionID === 'Allocations') {
totalAllocated = totalAllocated + Number(record.Values.Quantity);
}
}

if (totalAllocated !== soldQuantity) {
return five.createError(result, 'Total allocations do not match sold quantity');
}

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 CheckValidSell Function

The

CheckValidSell()
function needs to be attached to the Do Complete event on the Sells form. When the Save button is clicked on the Sells form, the current data on the Sell and Allocation forms are sent to the server, and the backend will call the function with the transactions being saved. If the Quantity fields do not match, an error will be returned and the data will not be saved in the database.

Path: Sells form > Do Complete 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 Sells record in the list and click the Events tab.


Events tab
Figure 6 - Events tab

3. Either click the Edit button in the form app bar or click directly in the Do Complete field.

4. Select CheckValidSell in the Do Complete field.


Do Complete field
Figure 7 - Do Complete field

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


Save button
Figure 8 - Save button

tip
This is a good time to deploy/run the Portfolio application to see how the
CheckValidSell()
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 9 - Down button

info

    We will still be working with the two buy TSLA records:

  • 2025-11-30 has a purchase of 10000 shares
  • 2025-12-02 has a purchase of 3000 shares

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 Price field, and press tab.


Add TSLA sell transaction
Figure 10 - Add TSLA sell transaction

5. Click the Add Allocations button.


Add Allocations button
Figure 11 - Add Allocations button

6. Select 2025-11-30 in the Buy field and type 10000 in the Quantity field.


Add allocation record
Figure 12 - Add allocation record

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


Save button
Figure 13 - Save button

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


Save button
Figure 14 - Save button

info
An error message is returned letting us know the total allocations do not match the sold quantity. This is because the sell quantity and the allocation quantity do not match.

9. Click the OK button.


Ok button
Figure 15- OK button

10. Click the Add Allocations button.


Add Allocations button
Figure 16 - Add Allocations button

11. Select 2025-12-02 in the Buy field and type 1000 in the Quantity field.


Add allocation record
Figure 17 - Add allocation record

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


Save button
Figure 18 - Save button

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


Save button
Figure 19 - Save button

14. Close the browser tab and return to Five.