Expand my Community achievements bar.

Join us in celebrating the outstanding achievement of our AEM Community Member of the Year!

Terminate a workflow programmatically and display custom error message on the page

Avatar

Level 1

Hi All,

 

I have a usecase to terminate the workflow initiated on a page if a condition check within a custom process step in that workflow is not met. Tried using terminate option but it doesn't have an option to send a custom error message that needs to displayed on the page as a reason for termination.

 

Please let me know if there is a way we can achieve it.

Thanks

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

4 Replies

Avatar

Level 8

Hi @sagarkmr,

If you are talking about /libs/cq/workflow/admin/console/content/failures.html page, you can take a look into:

  • /libs/cq/workflow/admin/console/components/item/workflowinstance/common.jsp
  • /libs/cq/workflow/admin/console/components/item/workflowinstance/workflowinstance.jsp

I see that it reads failureMessage property from the work item metadata map.

 

Try to do the following:

workItem.getMetaDataMap().put("failureMessage", "Custom message");
workflowSession.terminateWorkflow(workItem.getWorkflow());

 

Best regards,

Kostiantyn Diachenko.

Avatar

Level 1

Thanks konstantyn_diachenko. Will check this and confirm.

Avatar

Level 7

Hi @sagarkmr 

you can terminate a workflow using the  workflowSession.terminateWorkflow(workflow) approach to terminate the workflow programmatically and provide a user-visible message.

 

Workflow workflow = workflowSession.getWorkflow(item.getWorkflow().getId());
workflowSession.terminateWorkflow(workflow);

 

you can send an aem inbox notification as well using below code:

String message = "The provided path is invalid or does not exist: " + path;

InboxNotification inboxNotification = inboxNotificationSender.buildInboxNotification();
inboxNotification.setTitle("Invalid Path Notification");
inboxNotification.setContentPath(path);
inboxNotification.setAssignee(user);
inboxNotification.setMessage(message);

inboxNotificationSender.sendInboxNotification(adminResolver, inboxNotification);

 

or as @konstantyn_diachenko  suggested, You can utilize the workflow metadata (MetaDataMap) to set a termination message, which users can view in the Workflow Instances or AEM logs.

workflow.getMetaDataMap().put("terminationMessage", "The specified path does not exist: " + path);

 

Hope this helps!