Show/Hide Toolbars

Use TaskService when working with task records.

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

TaskService taskService = platform.getTaskService();

Creating a Task

The following sample creates a task record. During task creation, you can use API interface methods to set objects in the task record, including the current assignee, associated project, and task rate.

Project project = platform.getProjectService().read(0L);

User assignee = platform.getUserService().findUserByUsername("task assignee");

Task task = taskService.newTask("This is a new task", assignee);

task.setActualHours(BigDecimal.TEN);

task.setRateAmount(BigDecimal.ONE);

task.setNote("Note: This is a new task");

task.setProject(project);

Reading a Task

The following sample reads a task record. When you read a task, you retrieve the task so that you can make changes to it, such as updating or posting to it.

//the primary key of an existing task

long id = 12345678910L;

Task task = taskService.read(id);

Searching for Tasks

The following sample searches for tasks with a start date of January 2012.

Date startDate = new SimpleDateFormat("yyyy-MM-dd").parse("2012-01-01");

Date endDate = new SimpleDateFormat("yyyy-MM-dd").parse("2012-01-31");

DateCriterion dateCriterion = new DateCriterion(new FieldPath(Task.START_DATE));

 

List<Task> tasksStartingInJanuary2012 = taskService.search(new SearchCriteria(dateCriterion.between(startDate, endDate)));

Posting a Task

The following sample posts a task record. When you post a task, you can use API interface methods to update the task at the same time.

//the primary key of an existing task

long id = 12345678910L;

Task task = taskService.read(id);

task.setCompletedDate(new CalendarDate());

task.setCompletedPercent(BigDecimal.valueOf(100));

taskService.postTask(task);

Voiding a Task

The following sample voids a task record.

//the primary key of an existing task

long id = 12345678910L;

Task task = taskService.read(id);

taskService.voidTask(task);

Reassigning a Task

The following sample reassigns a task to another user.

//the primary key of an existing task

long taskId = 12345678910L;

Task task = taskService.read(taskId);

 

//the primary key of an existing user

long userId = 12345678910L;

User user = platform.getUserService().findUserByUsername("new assignee");

 

taskService.reassign(task, user);

Deleting a Task

The following sample deletes a task record.

//the primary key of an existing task

long taskId = 12345678910L;

Task task = taskService.read(id);

 

taskService.delete(task);

Getting a List of Transactions for a Task

The following sample returns a list of transactions for a task record.

//the primary key of an existing task

long taskId = 12345678910L;

Task task = taskService.read(id);

 

List<AccountTransaction> transactions = taskService.getTransactionsForTask(task);

Getting a List of Transactions for a Task associated with an Account

The following sample returns a list of transactions for a task record that is associated with an account.

//the primary key of an existing task

long taskId = 12345678910L;

Task task = taskService.read(id);

 

//the primary key of an existing account

long accountId = 12345678910L;

Account account = accountService.read(id);

 

List<AccountTransaction> transactions = taskService.getTransactionsForTask(account, task);