No Results
Search Tips:
- Please consider misspellings
- Try different search keywords
Newer Version Available
Step 5: Enable Input for New Expenses
-
In the ExpenseController class, enter this code below the
getExpenses() method.
1swfobject.registerObject("clippy.codeblock-0", "9");@AuraEnabled 2public static Expense__c saveExpense(Expense__c expense) { 3 upsert expense; 4 return expense; 5}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.
1swfobject.registerObject("clippy.codeblock-1", "9");createExpense : function(component, event, helper) { 2 var amtField = component.find("amount"); 3 var amt = amtField.get("v.value"); 4 if (isNaN(amt)||amt==''){ 5 amtField.setValid("v.value", false); 6 amtField.addErrors("v.value", [{message:"Enter an expense amount."}]); 7 } 8 else { 9 amtField.setValid("v.value", true); 10 var newExpense = component.get("v.newExpense"); 11 helper.createExpense(component, newExpense); 12 } 13},//Delimiter for future codecreateExpense validates the name and amount fields 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. Add these helper
functions after the updateTotal
function.
1swfobject.registerObject("clippy.codeblock-2", "9");createExpense: function(component, expense) { 2 this.upsertExpense(component, expense, function(a) { 3 var expenses = component.get("v.expenses"); 4 expenses.push(a.getReturnValue()); 5 component.set("v.expenses", expenses); 6 this.updateTotal(component); 7 }); 8}, 9 upsertExpense : function(component, expense, callback) { 10 var action = component.get("c.saveExpense"); 11 action.setParams({ 12 "expense": expense 13 }); 14 if (callback) { 15 action.setCallback(this, callback); 16 } 17 $A.enqueueAction(action); 18 } 19createExpense 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.