Initiating a Task Programmatically

The code sample below shows how to initiate a vacation request task programmatically:

  // create task object
  ObjectFactory objectFactory = new ObjectFactory();
  Task task = objectFactory.createTask();

  // set title
  task.setTitle("Vacation request for jcooper"); 

  // set creator
  task.setCreator("jcooper");
 
// set taskDefinitionId. taskDefinitionId is the target
// namespace of the task 
// If namespace is used, the active version of the composite corresponding 
// to that of the namespace will be used.
task.setTaskDefinitionId("http://xmlns.oracle.com/VacationRequest/
Project1/Humantask1");  (Your task definition ID will be different.)

  // create and set payload 
  Document document = XMLUtil.createDocument();
  Element payloadElem = document.createElementNS(TASK_NS, "payload"); 
  Element vacationRequestElem = document.createElementNS(VACATION_REQUEST_NS,
    "VacationRequestProcessRequest");
 
  Element creatorChild = document.createElementNS(VACATION_REQUEST_NS, "creator");
  creatorChild.appendChild(document.createTextNode("jcooper")); 
  vacationRequestElem.appendChild(creatorChild);
  
  Element fromDateChild = document.createElementNS(VACATION_REQUEST_NS, "fromDate");
  fromDateChild.appendChild(document.createTextNode("2006-08-05T12:00:00")); 
  vacationRequestElem.appendChild(fromDateChild);
  
  Element toDateChild = document.createElementNS(VACATION_REQUEST_NS, "toDate");
  toDateChild.appendChild(document.createTextNode("2006-08-08T12:00:00"));
  vacationRequestElem.appendChild(toDateChild);
  
  Element reasonChild = document.createElementNS(VACATION_REQUEST_NS, "reason");
  reasonChild.appendChild(document.createTextNode("Hunting")); 
  vacationRequestElem.appendChild(reasonChild);
  
  payloadElem.appendChild(vacationRequestElem);
  document.appendChild(payloadElem);
  
  task.setPayloadAsElement(payloadElem);
 
  IWorkflowServiceClient workflowServiceClient =
    WorkflowServiceClientFactory.getWorkflowServiceClient
    (WorkflowServiceClientFactory.SOAP_CLIENT);
  ITaskService taskService = workflowServiceClient.getTaskService(); 
  IInitiateTaskResponse iInitiateTaskResponse = taskService.initiateTask(task); 
  Task retTask = iInitiateTaskResponse.getTask(); 
  System.out.println("Initiated: " + retTask.getSystemAttributes().getTaskNumber() + " - " +
    retTask.getSystemAttributes().getTaskId());
  return retTask;