Expand my Community achievements bar.

Submissions are now open for the 2026 Adobe Experience Maker Awards.
SOLVED

Retrieving workflows instances for a specific model

Avatar

Level 1

Hi,

I am working in a multi tenant AEM instance. We are trying to retrieve workflow instances running/in any state for a particular model. We tried using getAllworkflows() which will give workflow instances of all the models. We tried few approaches but not working as expected. Can you please suggest any solution for the same?

 

AEM 6.5 with sp21

 

cc: @arunpatidar @aanchalsikka 

 

try (ResourceResolver resourceResolver = ResourceResolverUtils.getServiceResourceResolver(resourceResolverFactory, "testworkflowService")) {
logger.info("Fetching workflow instances using JCR query");
Session session = resourceResolver.adaptTo(Session.class);
List<Workflow> runningWorkflows = new ArrayList<>();
WorkflowSession wfSession = resourceResolver.adaptTo(com.adobe.granite.workflow.WorkflowSession.class);
try {
if (wfSession != null) {
Workflow[] wf = wfSession.getAllWorkflows();
}
}
}
1 Accepted Solution

Avatar

Correct answer by
Employee

To retrieve workflow instances for a particular model in AEM 6.5, you don't need to use getAllWorkflows(), as this returns all workflow instances irrespective of their model. Instead, you should use the filtering methods available in com.adobe.granite.workflow.WorkflowSession, specifically getWorkflows() which allows you to set a WorkflowQuery to filter by model ID and status.

Here’s what you should do:

  1. Use WorkflowSession.getWorkflows(WorkflowStatus status, String modelId) or build a WorkflowQuery:
    • The modelId should be the path to your workflow model (e.g., /var/workflow/models/myworkflow).
    • WorkflowStatus can be RUNNING, SUSPENDED, or COMPLETED depending on what you want to retrieve.

Example Code:

WorkflowSession wfSession = resourceResolver.adaptTo(com.adobe.granite.workflow.WorkflowSession.class);
String modelId = "/var/workflow/models/myworkflow"; // Set to your model path
List<Workflow> runningWorkflows = new ArrayList<>();

if (wfSession != null) {
    Workflow[] workflows = wfSession.getWorkflows(WorkflowStatus.RUNNING, modelId);
    runningWorkflows = Arrays.asList(workflows);
}

To retrieve all states, you may need to separately fetch RUNNING, SUSPENDED, and COMPLETED, or use the Workflow REST API for more advanced queries.

References:

If you’d like to perform JCR queries directly, you can look for workflow instances stored under /var/workflow/instances and filter by model attribute, but using the API as shown above is preferred and more robust.

View solution in original post

5 Replies

Avatar

Correct answer by
Employee

To retrieve workflow instances for a particular model in AEM 6.5, you don't need to use getAllWorkflows(), as this returns all workflow instances irrespective of their model. Instead, you should use the filtering methods available in com.adobe.granite.workflow.WorkflowSession, specifically getWorkflows() which allows you to set a WorkflowQuery to filter by model ID and status.

Here’s what you should do:

  1. Use WorkflowSession.getWorkflows(WorkflowStatus status, String modelId) or build a WorkflowQuery:
    • The modelId should be the path to your workflow model (e.g., /var/workflow/models/myworkflow).
    • WorkflowStatus can be RUNNING, SUSPENDED, or COMPLETED depending on what you want to retrieve.

Example Code:

WorkflowSession wfSession = resourceResolver.adaptTo(com.adobe.granite.workflow.WorkflowSession.class);
String modelId = "/var/workflow/models/myworkflow"; // Set to your model path
List<Workflow> runningWorkflows = new ArrayList<>();

if (wfSession != null) {
    Workflow[] workflows = wfSession.getWorkflows(WorkflowStatus.RUNNING, modelId);
    runningWorkflows = Arrays.asList(workflows);
}

To retrieve all states, you may need to separately fetch RUNNING, SUSPENDED, and COMPLETED, or use the Workflow REST API for more advanced queries.

References:

If you’d like to perform JCR queries directly, you can look for workflow instances stored under /var/workflow/instances and filter by model attribute, but using the API as shown above is preferred and more robust.

Avatar

Employee

What are you trying to do exactly? building a new user interface for workflow management? If yes, I'd recommend you review the  OOTB workflow console, JMX Console or ACS Commons Bulk Workflow Manager to assess if they fulfill your requirements.

 

If you need a query for adhoc analysis, you can use something like this

# Find workflow instances of the request_for_activation model started after a specific date.

http://localhost:4502/bin/querybuilder.json?path=/var/workflow/instances&p.limit=-1&1_property=jcr:primaryType&1_property.value=cq:Workflow&2_property=modelId&2_property.value=/var/workflow/models/request_for_activation&p.hits=full&p.nodedepth=1&daterange.property=startTime&daterange.lowerBound=2024-04-24

 

 

Avatar

Community Advisor

@MithraDe if you just need this list, You can create a ACS commons report to look for all workflow instances under /var/workflow/instances.

You can then filter it based on attributes like modeId, status, start_time etc.

Hope this helps.

Avatar

Level 5

Hi @MithraDe ,

You can retrieve all workflows using getAllWorkflows() and then filter them based on the workflow model:-

 

List<Workflow> wfModels = new ArrayList<>();
WorkflowSession wfSession = resourceResolver.adaptTo(WorkflowSession.class);

if (wfSession != null) {
Workflow[] allWorkflows = wfSession.getWorkflows(WorkflowStatus.RUNNING);
wfModels = Arrays.stream(allWorkflows)
.filter(wf -> wf.getWorkflowModel().getId().equals(modelPath)) 
.collect(Collectors.toList());
}

 

Try this and let me know if it works.

Thanks.

Avatar

Community Advisor

Hi @MithraDe 

 

You can try getActiveWorkflowItems in conjuction with WorkItem filter to list down active workitem for a model

 

ResultSet<WorkItem> getActiveWorkItems(long start,
                                       long limit,
                                       WorkItemFilter filter)
                                throws WorkflowException

  

WorkItemFiter example

package com.example.aem.workflow;

import com.adobe.granite.workflow.exec.WorkItem;
import com.adobe.granite.workflow.exec.filter.WorkItemFilter;

import org.osgi.service.component.annotations.Component;

/**
 * Custom WorkItemFilter that checks if a WorkItem belongs to a specific workflow model.
 */
@Component(service = WorkItemFilter.class)
public class WorkflowModelFilter implements WorkItemFilter {

    private static final String TARGET_WORKFLOW_MODEL_PATH = "/var/workflow/models/my-custom-workflow/jcr:content/model";

    @Override
    public boolean accept(WorkItem workItem) {
        if (workItem == null) {
            return false;
        }

        String modelId = workItem.getWorkflow().getWorkflowModel().getId();
        // Compare the workflow model ID with the expected model path
        return TARGET_WORKFLOW_MODEL_PATH.equals(modelId);
    }
}
Arun Patidar

AEM LinksLinkedIn