How to write a custom function usable in JUEL expressions

I guess we could investigate this from scratch ourself, but maybe we can win some time if somebody could point us to the correct steps: What should be done to make a custom function/service available in JUEL expressions in the BPMN process?

Example usecase: I have a process that contains a mail task that should be sent to a user involved in the process. What we currently do: in one of the preceeding steps before the mail task, we store the required fields as separate variables (so at least three process variables: ‘useremail’, ‘userfirstname’ and ‘userlastname’).

This is somewhat redundant, though, because the database contains users with mail, first, lastname as attributes. I would like to make it possible for our users to configure a JUEL expression, that, for example, would be,

  • for the ‘To’ field of the mail task:: ${FunkyUserService.getUser(username).getEmailAddress()}
  • for the ‘text’ (mail body) “Hello {FunkyUserService.getUser(username).getFirstname()} {FunkyUserService.getUser(username).getLastname()}”
  • … or even better "${SomeUserService.getUser(username).getFullName()} "

… and obviously this FunkyUserService would use our own internal userService to fetch the user’s attributes at runtime from the db, when the JUEL expression is evaluated.
Because, who knows, maybe the user’s email adress has changed between storing the initial useremail procesvariable, and the time the email gets sent.

(Note that we use flowable as a platform ; our users don’t write java code themselves so we want to make “FunkyUserService” available as a JUEL expression)

And, it would be very nice to have this same function / expression working in the DMN decisiontable JUEL expression as well.

What steps should be taken, roughly, to achieve this?

If you are using Spring, you can access any bean by it’s id in a JUEl expression. So if FunkyUserService is a Spring bean, you will automatically be able to access it. If you want to restrict it or tightly control which beans are available, have a look at the ‘beans’ property of the engine configuration.

Besides that, it’s possible to define custom functions in both bpmn and cmmn. Have look at the following test files, where a custom JUEL function is configured and used in DMN (for BPMN it is similar):

all right, many thanks Joram!

Hi Pieter,

I am trying to achieve something very similar using Springboot ,Java and JUEL expressions . Any progress you made so far on this ? Any links that might help?