Terminate a workflow programmatically and display custom error message on the page | Community
Skip to main content
January 8, 2025
Solved

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

  • January 8, 2025
  • 3 replies
  • 850 views

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by konstantyn_diachenko

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.

3 replies

konstantyn_diachenko
Community Advisor
konstantyn_diachenkoCommunity AdvisorAccepted solution
Community Advisor
January 8, 2025

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.

Kostiantyn Diachenko, Community Advisor, Certified Senior AEM Developer, creator of free AEM VLT Tool, maintainer of AEM Tools plugin.
sagarkmrAuthor
January 10, 2025

Thanks konstantyn_diachenko. Will check this and confirm.

abhishekanand_
Community Advisor
Community Advisor
January 8, 2025

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!

Abhishek Anand
sagarkmrAuthor
January 10, 2025

Thanks abhishekanand_. Will try this.

kautuk_sahni
Community Manager
Community Manager
January 27, 2025

@sagarkmr Did you find the suggestions helpful? Please let us know if you require more information. Otherwise, please mark the answer as correct for posterity. If you've discovered a solution yourself, we would appreciate it if you could share it with the community. Thank you!

Kautuk Sahni