Show/Hide Toolbars

Use ExpenseService when working with expense records.

Whenever a Service operation requires ExpenseService, set up ExpenseService using the following code:

ExpenseService expenseService = platform.getExpenseService();

Creating an Expense

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));

Updating an Expense

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));

Reading an Expense

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);

Posting an Expense

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);

Voiding an Expense

The following sample voids an expense record.

//An expense must be posted to be voided

expenseService.voidExpense(expense);

Deleting an Expense

The following sample deletes an expense record.

expenseService.delete(expense);

Getting Transactions for an Expense

The following sample returns a list of transactions for an expense record.

List<AccountTransaction> transactions = expenseService.getTransactionsForExpense(expense);

Getting Transactions for an Expense Associated with an Account

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);

Searching for Expenses

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));