cancel
Showing results for 
Search instead for 
Did you mean: 

Can we approve a task through Java API?

satheeshkumar
Champ in-the-making
Champ in-the-making
Hi All,

I have a requirement, like one of the user task, say approve/reject task needs to be done automatically thorough Java API.
I have written a code as shown below, to retrieve the list of tasks for a particular user and tried to approve programatically.


List<WorkflowTask> workflowTaskList =  workflowService.getPooledTasks(serviceRegistry.getAuthenticationService().getCurrentUserName());
      
      
      
      for(WorkflowTask wfTask : workflowTaskList){
         
         try {
            Map<QName, Serializable> wfProps = new HashMap<>();
            //Approving the content
            wfProps.put(HCCustomModel.QNAME_HC_REVIEW_OUTCOME, HCCustomModel.REVIEW_APPROVE); //Adding "Approve" workflow property
            workflowService.updateTask(wfTask.getId(), wfProps, null, null);
            wfProps = wfTask.getProperties();
            for(QName key : wfProps.keySet()){
               logger.info("Key : "+key+" , Value : "+wfProps.get(key));
            }
            workflowService.endTask(wfTask.getId(), null);
         } catch (AlfrescoRuntimeException e) {
            logger.error("An error occured while processing the task:"+wfTask.getId(), e);            
         }
      }


But when I execute, the task always gets rejected(as "Reject" is the default action for the task), even though I update the task with "Approve" workflow property.
Can any one point me out what I am doing wrong here.
Or, the approval can't be done through Java API?

Any guidance/suggestions would be greatly helpful to me.

Thanks and Regards,
Satheesh D
2 REPLIES 2

romschn
Star Collaborator
Star Collaborator
You should simply try using endTask method passing the taskId and review outcome as Approve. In your code snippet, you are passing null in the endTask method. Hence, it is taking the default Reject as the outcome.

Hope this helps.

Thanks romchn, that works perfectly.