Search code examples
jbpm

In a JBPM script task can I access a process variables last updated information


I have trawled through the JBPM 7 documentation but struggling to find a solution.

In my JBPM process (specifically in a Script Task process - language Java) I need to access a process variables last updated time and make a decision based on this value i.e. if value greater than 2 hours perform another action. Does anybody know if/how it is possible to access the process variables last update time?

I can access the process variable value using the following

String status = kcontext.getVariable("overallStatus"));

And this returns the value of the overallStatus process variable but I can't seem to get hold of the last modification time (importantly within the script task!), I know this information is stored against the process variable as it is show in the business process GUI:

enter image description here


Solution

  • I know of a solution using a custom workItemHandler, I'll provide the details below hopefully you can substitute the Script Task with a custom workItemHandler. A list of VariableInstanceLog objects can be obtained from the AuditLogService for a given processVariable, from each of these audit objects you can access the last modification date.

    The logic of your required check can then be implemented in the custom workItemHandler passing the result to the process instance.

    Here's a snippet of example of code that will print out the last modification date of a process variable called 'TheResult' in the current process instance.

    public class CustomWorkItemHandler extends AbstractWorkItemHandler {
    
     private AuditLogService auditLogService; 
    
    public CustomWorkItemHandler(KieSession ksession) {
        super(ksession);
    }
    
    public void executeWorkItem(WorkItem workItem,
                                WorkItemManager manager) {
      
            // sample parameters
            String sampleParam = (String) workItem.getParameter("SampleParam");
            String sampleParamTwo = (String) workItem.getParameter("SampleParamTwo");
    
            // complete workitem impl...
            
            auditLogService = new JPAAuditLogService(getSession().getEnvironment());
            
            //TheResult - is the process variable to check
            List<VariableInstanceLog> list = auditLogService.findVariableInstances(workItem.getProcessInstanceId(), "TheResult");
            if (list.size() > 0){
                VariableInstanceLog log = list.get(list.size()-1);
                System.out.println("Last Recorded Date:" + log.getDate());
            }
            
            // return results
            String sampleResult = "A result to return here";
            Map<String, Object> results = new HashMap<String, Object>();
            results.put("SampleResult", sampleResult);
    
    
            manager.completeWorkItem(workItem.getId(), results);
       
    }
    
    @Override
    public void abortWorkItem(WorkItem workItem,
                              WorkItemManager manager) {
        // stub
    }
    

    }

    You will need to add the packaged custom workItemHandler jar as an artifact through Business-Central and register your workItemhandler passing the ksession to the constructor in your project->settings->Deployments->WorkItemHandlers e.g.

    HandlerNameHere -- new org.jbpm.contrib.CustomWorkItemHandler(ksession) -- MVEL

    More details on building a custom WorkItemHandler, using an maven archetype, can be found here http://mswiderski.blogspot.com/2018/04/jbpm-work-items-are-really-simple.html And to incorporate the customWorkItemHandler into your process see this short video https://www.youtube.com/watch?v=_XIZ0KRTahE