How to assign a workflow dynamically in AEM

Photo by Campaign Creators on Unsplash

Very often when creating custom workflows in Adobe Experience Manager (AEM) we need to be able to request input from a user. AEM support out of the box assigning directly to users or groups. Many times you want to be add some business rules on who to assign.

Dynamic Participant Step

AEM comes with a Dynamic Participant Step Process that allows you to build your own code to assign the workflow. 

Dynamic Participant tep
Dynamic Participant Step in AEM

Firstly you need to implement the interface ParticipantChooser:

import com.adobe.granite.workflow.WorkflowException;
import com.adobe.granite.workflow.WorkflowSession;
import com.adobe.granite.workflow.exec.ParticipantStepChooser;
import com.adobe.granite.workflow.exec.WorkItem;
import com.adobe.granite.workflow.metadata.MetaDataMap;
import org.osgi.service.component.annotations.Component;

@Component(
    service = ParticipantStepChooser.class,
    property = {
        ParticipantStepChooser.SERVICE_PROPERTY_LABEL 
        + "=My custom participant step chooser"
    })
public class MyParticipantChooser 
    implements ParticipantStepChooser {

  @Override
  public String getParticipant(WorkItem workItem, 
      WorkflowSession workflowSession,
      MetaDataMap metaDataMap) throws WorkflowException {
    
      /* return the id of Authorizable to assign the 
       workflow to (either user or group) */
  }
}

Then use the dynamic participant chooser block available in AEM and choose your custom Participant chooser. 

AEM workflow
AEM Workflow

Once you deploy your code to AEM, and option will be added to the Participant Chooser dropdown.

Dynamic Participant Step dialog in AEM

This is very helpful if you want to extract which user to assign depending on the page level, workflow initiator, or any other business requirement.  For example, you can choose a participant group based on one field of your page properties, the path of the website, the page name, etc.

Leave a Reply