Newer Version Available
Step 5: Enable Input for New Expenses
The following flowchart shows the flow of data in your app when you create a new expense.
The data is captured when you click the Submit button in the component
form.cmp, processed by your JavaScript code
and sent to the server-side controller to be saved as a record. Data from the
records is displayed in the nested component you created in the previous step.![]()
-
In the ExpenseController class, enter this code below the
getExpenses() method.
The saveExpenses() method enables you to insert or update an expense record using the upsert operation.
-
Create the controller-side actions to create a new expense record when the
Submit button is pressed. In
formController.js, add this code after the doInit action.
createExpense validates the amount field using default error handling, which appends an error message represented by ui:inputDefaultError. The controller invalidates the input value using setValid(false) and clears any errors using setValid(true). For more information on field validation, see Validating Fields.
Notice that you’re passing in the arguments to a helper function helper.createExpense(), which then triggers the Apex class saveExpense. -
Create the helper function to handle the record creation. In
formHelper.js, add these helper functions after the
updateTotal function.
createExpense calls upsertExpense, which defines an instance of the saveExpense server-side action and sets the expense object as a parameter. The callback is executed after the server-side action returns, which updates the records, view, and counters. $A.enqueueAction(action) adds the server-side action to the queue of actions to be executed.
-
Save your changes and reload your browser. Test your app by entering Breakfast, 10, ABC Co., Apr 30, 2014 9:00:00
AM. For the date field, you can also use the date picker to set a date
and time value. Click the Submit button. The record is added to both your
component view and records, and the counters are updated.