Skip to content

Instantly share code, notes, and snippets.

@ejlp12
Last active March 19, 2025 17:55
Show Gist options
  • Save ejlp12/347d20ceada74f1bba29387baaf40a86 to your computer and use it in GitHub Desktop.
Save ejlp12/347d20ceada74f1bba29387baaf40a86 to your computer and use it in GitHub Desktop.
Jboss bpms, jbpm, brms, script, onexit, onentry

Get and set process variable:

// Set variable
kcontext.setVariable("XYZ", "9000001214"); 

// Get variable
System.out.println("\t##### Process variable XYZ: " + 
                  kcontext.getVariable("XYZ"));
                  
                

On-Exit action example:

System.out.println("Entering " + kcontext.getNodeInstance().getNodeName() +" Node");

Vehicle vehicle = (Vehicle)((org.jbpm.workflow.instance.node.WorkItemNodeInstance)kcontext.getNodeInstance()).getWorkItem().getParameter("vehicle");
System.out.println(vehicle.getModel());

System.out.println("Actor ID: " + ((org.jbpm.workflow.instance.node.HumanTaskNodeInstance)(kcontext.getNodeInstance())).getWorkItem().getResult("ActorId") );

NOTE: You cannot getWorkItem() in onEntry script, it will result null. Because the WorkItem is not created yet... I guess

Getting output variable names:

HumanTaskNodeInstance nodeInstance = (HumanTaskNodeInstance) kcontext.getNodeInstance();
HumanTaskNode htNode = nodeInstance.getHumanTaskNode();
Map<String,String> outMap = htNode.getOutMappings();

for (Map.Entry<String, String> entry : outMap.entrySet()) {
    String key = entry.getKey();
    Object value = entry.getValue();
    System.out.println("output task variable "+key+" is mapped to the process variable"+value);

}

Sending event

//kcontext.getKnowledgeRuntime().signalEvent(eventType, data, kcontext.getProcessInstance().getId());

kcontext.getKnowledgeRuntime().signalEvent("TEST_EVENT", null, kcontext.getProcessInstance().getId());

https://access.redhat.com/solutions/1166033

@ejlp12
Copy link
Author

ejlp12 commented Nov 17, 2016

System.out.println("### Task1 onEntry");


String var2 = (String) kcontext.getVariable("var1");
System.out.println("### Process variable var1: " + var2);

System.out.println("### NodeInstance: " + kcontext.getNodeInstance());

// If you put this code in onEntry script this line (WorkItem) will result "null"
System.out.println("### WorkItem: " + 
 ((org.jbpm.workflow.instance.node.WorkItemNodeInstance)kcontext.getNodeInstance()).getWorkItem());


if ( (((org.jbpm.workflow.instance.node.WorkItemNodeInstance)kcontext.getNodeInstance()).getWorkItem()) != null ) {
  
  long id = ((org.jbpm.workflow.instance.node.WorkItemNodeInstance)kcontext.getNodeInstance()).getWorkItem().getId();
System.out.println("### Task1 ID: " + id);
  
}

@ejlp12
Copy link
Author

ejlp12 commented Jul 19, 2018

Rule Task onExit action script example:

// Error because getRuleSetNode() is protected method
LeaveRequest req = (LeaveRequest)((org.jbpm.workflow.instance.node.RuleSetNodeInstance)kcontext.getNodeInstance()).getRuleSetNode().getParameter("outLeaveRequest");

validRequest = (req.getStatus() == "validated");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment