Always send an email when a task is created

I have found example of attaching task listener so that an email is sent when the task is created.

Is it possible to have this behaviour for all processes, without attaching the delegate specifically to a task? Also, there will be some user tasks that can be created on the tasklist, is there a way to do that?

Hi @Edmondo_Porcu
Have a look at below link
https://docs.camunda.org/manual/7.5/user-guide/cdi-java-ee-integration/the-cdi-event-bridge/

Thanks Hassang,

that’s what I was looking for. I think it’s misleading that it is under cdi-java-ee as we are not using any java-ee at all.

How do I retrieve then the emails of all the potential assignee for that tasks?

Thank you

Edmondo

You could also take a look at using Process Application Event Listener, in that case you’d have direct access to the task object.

Hi @Edmondo_Porcu

The observer method has the parameter
BusinessProcessEvent businessProcessEvent
You could get the delegate task through
businessProcessEvent.getTask() from where you could get the assignee by calling getAssignee()

https://docs.camunda.org/javadoc/camunda-bpm-platform/7.5/org/camunda/bpm/engine/cdi/BusinessProcessEvent.html

You could get the email from user profile as in below example
https://blog.camunda.org/post/2013/10/how-to-send-email-when-usertask-is/

Hi @Edmondo_Porcu,

you can also use the extension Camunda BPM Reactor and register a listener for task events.

Best regards,
Philipp

1 Like

Hi … the reactor extension would be my tool of choice here as well, but I as the author, am biased … what you can do, if you want to simulate the behavior:

  • add a custom BpmnParseListener (either directly to the configuration or via ProcessEnginePlugin#preInit()
  • in that parse listener, register your “send eMail notification” code as a taskListener for task#create:
BpmnParseListener emailParseListener = new AbstractBpmnParseListener(){
    @Override
    public void parseUserTask(Element userTaskElement, ScopeImpl scope, ActivityImpl activity) {
            TaskDefinition taskDefinition = ((UserTaskActivityBehavior) activity.getActivityBehavior())
                                                                        .getTaskDefinition();
            taskDefinition.addTaskListener(TaskListener.CREATE, new TaskListener(){
                     @Override
                     public void notify(DelegateTask delegateTask) {
                               // your email code here
                     }
            });
    }
 };

processEngineConfiguration.getCustomPostBPMNParseListeners().add(emailParseListener);
1 Like

I have implemented your advice but the getTaskHandler on my ServletApplication never gets invoked. Do I need to add something else?

I would need some code/more info, please.

`
class ScalatraBootstrap extends AbstractProcessApplication with LifeCycle {

val taskListener = buildTaskListener

override def init(context: ServletContext) {
val defaultProcessEngine = RuntimeContainerDelegate.INSTANCE.get().getProcessEngineService.getDefaultProcessEngine
bootstrap(defaultProcessEngine)
this.deploy()
}

override def getTaskListener: TaskListener = taskListener

override val autodetectProcessApplicationName: String = “credimi-workflows”

override def getReference: ProcessApplicationReference = {
atomicReference.compareAndSet(null, new ProcessApplicationReferenceImpl(this))
atomicReference.get()
}

override def destroy(context: ServletContext): Unit = {
logger.info(“Undeploying app”)
this.undeploy()
}
}

`
This is our simple Servlet, which we deploy as a war on a tomcat where we installed a shared process engine. The getTaskListener method is never invoked