Guide to reversing approval processes: simple steps to ensure a smooth process
About 390 wordsAbout 1 minDecember 2, 2024
1. Retrieve Process Instance
When implementing the cancellation function, the first step is to obtain the process instance to be canceled. You can find the process instance using the process instance ID or business key:
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RuntimeService runtimeService = processEngine.getRuntimeService();
ProcessInstance processInstance = runtimeService.createProcessInstanceQuery()
.processInstanceId("processInstanceId")
.singleResult();
2. Terminate the Process Instance
After obtaining the process instance, you can call the deleteProcessInstance
method to terminate the process:
if (processInstance != null) {
runtimeService.deleteProcessInstance(processInstance.getId(), "Cancel approval process");
System.out.println("Process instance has been canceled: " + processInstance.getId());
} else {
System.out.println("Process instance not found");
}
3. Update Database Tables
After terminating the process instance, you also need to update the relevant records in the database to ensure data consistency. Assuming we have created the following database tables:
- Process_Instances: Stores information about process instances
- Approval_Tasks: Stores information about approval tasks
- Approval_Logs: Stores logs of approval actions
We need to update the records in these tables to reflect the cancellation operation. For example:
-- Update the process instances table to set the status to canceled
UPDATE Process_Instances
SET Status = 'CANCELLED', Updated_At = NOW()
WHERE Process_Instance_ID = ?;
-- Update the approval tasks table to set the relevant tasks to canceled
UPDATE Approval_Tasks
SET Status = 'CANCELLED', Completed_At = NOW()
WHERE Process_Instance_ID = ?;
-- Log the cancellation action
INSERT INTO Approval_Logs (Task_ID, Approver_ID, Action, Timestamp, Comments)
VALUES (?, ?, 'CANCELLED', NOW(), 'Process has been canceled');