Obtaining Workflow Objects in ECMA Scripts
As described in Locating the Script, AEM (via Apache Sling) provides an ECMA script engine that executes server-side ECMA scripts. The org.apache.sling.scripting.core.ScriptHelper class is immediately available to your scripts as the sling variable.
The ScriptHelper class provides access to the SlingHttpServletRequest that you can use to eventually obtain the WorkflowSession object; for example:
Using the Workflow REST API
The Workflow console makes heavy use of the REST API; so this page describes the REST API for workflows.
The following actions are supported with the REST API:
- start or stop a workflow service
- create, update or delete workflow models
- start, suspend, resume or terminate workflow instances
- complete or delegate work items
POST request.In this page it is assumed that AEM runs on localhost at port 4502 and that the installation context is " /" (root). If it is not the case of your installation, the URIs, to which the HTTP requests apply, need to be adapted accordingly.
The rendering supported for GET requests is the JSON rendering. The URLs for GET should have the .json extension, for example:
http://localhost:4502/etc/workflow.json
Managing Workflow Instances
The following HTTP request methods apply to:
http://localhost:4502/etc/workflow/instances
Managing a Workflow Instance by its State
The following HTTP request methods apply to:
http://localhost:4502/etc/workflow/instances.{state}
| HTTP request method | Actions |
|---|---|
GET | Lists the available workflow instances and their states ( RUNNING, SUSPENDED, ABORTED or COMPLETED) |
Managing a Workflow Instance by its ID
The following HTTP request methods apply to:
http://localhost:4502/etc/workflow/instances/{id}
Managing Workflow Models
The following HTTP request methods apply to:
http://localhost:4502/etc/workflow/models
Example: in the browser, a request to http://localhost:4502/etc/workflow/models.json generates a json response similar to the following:
Managing a Specific Workflow Model
The following HTTP request methods apply to:
http://localhost:4502*{uri}*
Where *{uri}* is the path to the model node in the repository.
Example: in the browser, a request to http://localhost:4502/var/workflow/models/publish_example.json returns a json response that is similar to the following code:
Managing a Workflow Model by its Version
The following HTTP request methods apply to:
http://localhost:4502/etc/workflow/models/{id}.{version}
| HTTP request method | Actions |
|---|---|
GET | Gets the data of the model in the given version (if it exists). |
Managing (User) Inboxes
The following HTTP request methods apply to:
http://localhost:4502/bin/workflow/inbox
Managing a (User) Inbox by the WorkItem ID
The following HTTP request methods apply to:
http://localhost:4502/bin/workflow/inbox/{id}
| HTTP request method | Actions |
|---|---|
GET | Gets the data (definition and metadata) of the inbox WorkItem identified by its ID. |
Examples
How to get a List of all Running Workflows with their IDs
To get a list of all running workflows, do a GET to:
http://localhost:4502/etc/workflow/instances.RUNNING.json
How to get a List of all Running Workflows with their IDs - REST using curl
Example using curl:
The uri displayed in the results can be used as the instance id in other commands; for example:
How to change the Workflow Title
To change the Workflow Title displayed in the Instances tab of the workflow console, send a POST command:
-
to:
http://localhost:4502/etc/workflow/instances/{id} -
with the following parameters:
action: its value has to be:UPDATEworkflowTitle: the workflow title
How to change the Workflow Title - REST using curl
Example using curl:
How to List all Workflow Models
To get a list of all available workflow models, do a GET to:
http://localhost:4502/etc/workflow/models.json
How to List all Workflow Models - REST using curl
Example using curl:
Obtaining a WorkflowSession Object
The com.adobe.granite.workflow.WorkflowSession class is adaptable from a javax.jcr.Session object or a org.apache.sling.api.resource.ResourceResolver object.
Obtaining a WorkflowSession Object - Java
In a JSP script (or Java code for a servlet class), use the HTTP request object to obtain a SlingHttpServletRequest object, which provides access to a ResourceResolver object. Adapt the ResourceResolver object to WorkflowSession.
Obtaining a WorkflowSession Object - ECMA Script
Use the sling variable to obtain the SlingHttpServletRequest object that you use to obtain a ResourceResolver object. Adapt the ResourceResolver object to the WorkflowSession object.
Creating, Reading or Deleting Workflow Models
The following examples show how to access workflow models:
- The code for Java and ECMA script uses the
WorkflowSession.createNewModelmethod. - The curl command accesses the model directly using its URL.
The examples used:
- Create a model (with the ID
/var/workflow/models/mymodel/jcr:content/model). - Delete the model.
deleted property of the model’s metaData child node to true.When creating a model:
-
The workflow model editor requires that models use a specific node structure below
/var/workflow/models. The parent node of the model must be of the typecq:Pagehaving ajcr:contentnode with the following property values:sling:resourceType:cq/workflow/components/pages/modelcq:template:/libs/cq/workflow/templates/model
When you create a model, you must first create this
cq:Pagenode and use itsjcr:contentnode as the parent of the model node. -
The
idargument that some methods require for identifying the model is the absolute path of the model node in the repository:/var/workflow/models/<*model_name>*/jcr:content/modelNOTE
Creating, Reading or Deleting Workflow Models - Java
Creating, Reading or Deleting Workflow Models - ECMA Script
Deleting a Workflow Model - REST using curl
Filtering Out System Workflows when Checking Workflow Status
You can use the WorkflowStatus API to retrieve information about the workflow status of a node.
Various methods have the parameter:
excludeSystemWorkflows
This parameter can be set to true to indicate that system workflows should be excluded from the relevant results.
You can update the OSGi configuration Adobe Granite Workflow PayloadMapCache that specifies the workflow Models to be considered as system workflows. The default (runtime) workflow models are:
/var/workflow/models/scheduled_activation/jcr:content/model/var/workflow/models/scheduled_deactivation/jcr:content/model
Auto-Advance Participant Step after a Timeout
If you need to auto-advance a Participant step that has not been completed within a predefined time you can:
-
Implement an OSGI event listener to listen on task creation and modification.
-
Specify a timeout (deadline), then create a scheduled sling job to fire at that time.
-
Write a job handler that is notified when the timeout expires and triggers the job.
This handler will take the required action on the task if the task is not yet completed
Interacting with Workflow Instances
The following provide basic examples of how to interact (progammatically) with workflow instances.
Interacting with Workflow Instances - Java
Interacting with Workflow Instances - ECMA Script
Interacting with Workflow Instances - REST using curl
-
Starting a workflow
-
Listing the instances
This will list all instances; for example:
NOTE
See How to get a List of all Running Workflows with their IDs for listing instances with a specific status. -
Suspending a workflow
-
Resuming a workflow
-
Terminating a workflow instance
Interacting with Work Items
The following provide basic examples of how to interact (progammatically) with work items.
Interacting with Work Items - Java
Interacting with Work Items - ECMA Script
Interacting with Work Items - REST using curl
-
Listing Work Items from the Inbox
Details for work items currently in the Inbox will be listed; for example:
-
Delegating Work Items
NOTE
Thedelegateemust be a valid option for the workflow step. -
Completing or advancing work items to the next step
Listening for Workflow Events
Use the OSGi event framework to listen for events that the com.adobe.granite.workflow.event.WorkflowEvent class defines. This class also provides several useful methods for obtaining information about the subject of the event. For example, the getWorkItem method returns the WorkItem object for the workitem that is involved in the event.
The following example code defines a service that listens to workflow events and performs tasks according to the type of event.
Was this content helpful?
Announcements
Get hands-on with agentic AI.
Explore how AI agents work together through real examples and hands-on practice. Join your local User Group to design an agent workflow with your peers and leave with a practical framework you can apply to your own work.
Find an eventAdobe Experience Manager Roadmap Webinar, Part 1 (Sites & Assets) - Americas & EMEA
Preview the AEM Sites and Assets roadmap and agentic innovations. Ask the team live.
Adobe Experience Manager Roadmap Webinar, Part 1 (Sites & Assets) - Americas & APAC
Preview the AEM Sites and Assets roadmap and agentic innovations. Ask the team live.
Adobe Brand Visibility Roadmap Webinar - Americas & EMEA
Preview the Brand Visibility roadmap across Brand Concierge and AEM Sites. Ask live.