Use ExpenseService when working with expense records.
Whenever a Service operation requires ExpenseService, set up ExpenseService using the following code:
ExpenseService expenseService = platform.getExpenseService(); |
The following sample creates an expense record. During expense creation, you can use API interface methods to set objects in the expense record, including the user who is responsible for the expense and a description of the expense.
//Expense description is a mandatory field Expense expense = expenseService.newExpense("Brand New Test Description"); User user = platform.getUserService().findUserByUsername("Expenser"); expense.setExpensedBy(user); //Date will be converted to midnight GMT, then back to the local date/time relative to GMT midnight Calendar cal = Calendar.getInstance(); cal.set(2014, 01, 30); expense.setExpenseDate(new CalendarDate(cal)); |
The following sample updates an expense record. When you update an expense, you can use API interface methods to update objects in the expense, including the expense name.
expense.setShortDescription("Expense Description Update"); //Adding new unit price for expensed item ($150) and quantity of units (2) expense.setUnitPrice(BigDecimal.valueOf(150)); expense.setQuantity(BigDecimal.valueOf(2)); |
The following sample reads an expense record. When you read an expense, you can retrieve the last saved version of the expense to post, delete, or do something else with the record.
//The expenseHandle here is a preexisting Expense object Expense lastSavedExpense = expenseService.readLastSaved(expenseHandle); |
The following sample posts an expense record. When you post an expense, you can use API interface methods to prepare an expense for posting.
//Ensure that these options are enabled in an account to post an expense account.setAllowPosting(true); account.getAccountPostingCriteria().setAllowExpense(true); expenseService.postExpense(expense); |
The following sample voids an expense record.
//An expense must be posted to be voided expenseService.voidExpense(expense); |
The following sample deletes an expense record.
expenseService.delete(expense); |
The following sample returns a list of transactions for an expense record.
List<AccountTransaction> transactions = expenseService.getTransactionsForExpense(expense); |
The following sample returns a list of transactions for an expense record that is associated with an account.
List<AccountTransaction> transactions = expenseService.getTransactionsForExpense(account, expense); |
The following sample searches for expenses with a start date of January 2012.
Calendar cal = Calendar.getInstance(); cal.set(2012, 1, 1); Date startDate = cal.getTime();
Calendar cal = Calendar.getInstance(); cal.set(2012, 1, 31); Date endDate = cal.getTime();
DateCriterion dateCriterion = new DateCriterion(Expense.EXPENSE_DATE).between(startDate, endDate);
List<Expense> expenses = platform.getExpenseService().search(new SearchCriteria(dateCriterion)); |