Expand my Community achievements bar.

SOLVED

How to programmatically advance to next step if a Participant hasn't acted on the workflow.

Avatar

Level 4

I have 3 steps in workflow model:

1. Participant

2. Participant

3. Process

If the participant hasn't acted on the workflow request from their inbox for some time period, I need to programmatically advance the workflow to the next step. Please help.

Topics

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

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hello @rakesh_h2 

 

You can use the "Timeout Handler" configuration in participant step to achieve the same.

 

Set "Timeout Handler" = "Absolute Time Auto Advancer". And set "Timeout" to some value

 

https://experienceleague.adobe.com/docs/experience-manager-65/developing/extending-aem/extending-wor...


Aanchal Sikka

View solution in original post

5 Replies

Avatar

Community Advisor

Hello @rakesh_h2 - 

 

You can try below approach : 

 

 

import java.util.Calendar;
import javax.jcr.Node;
import com.adobe.granite.workflow.exec.WorkItem;
import com.adobe.granite.workflow.exec.Workflow;
import com.adobe.granite.workflow.exec.WorkflowData;
import com.adobe.granite.workflow.exec.WorkflowException;
import com.adobe.granite.workflow.exec.WorkflowProcess;
import com.adobe.granite.workflow.metadata.MetaDataMap;
import com.adobe.granite.workflow.WorkflowSession;
import com.day.cq.workflow.WorkflowException;

public class CustomWorkflowProcessStep implements WorkflowProcess {
  
  private static final String PARTICIPANT_ID = "participantId";
  private static final String INACTIVITY_THRESHOLD = "inactivityThreshold";

  @Override
  public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap) throws WorkflowException {
    try {
      WorkflowData workflowData = workItem.getWorkflowData();
      String participantId = metaDataMap.get(PARTICIPANT_ID, String.class);
      int inactivityThreshold = metaDataMap.get(INACTIVITY_THRESHOLD, Integer.class);

      Workflow workflow = workItem.getWorkflow();
      String workflowInstancePath = workflow.getInitiator().concat("/").concat(workflow.getId());

      Calendar currentTime = Calendar.getInstance();
      Calendar participantStepTime = getParticipantStepTime(workflowSession, workflowInstancePath, participantId);
      
      if (participantStepTime != null) {
        long elapsedTimeInMillis = currentTime.getTimeInMillis() - participantStepTime.getTimeInMillis();
        long inactivityThresholdInMillis = inactivityThreshold * 60 * 1000;
        if (elapsedTimeInMillis >= inactivityThresholdInMillis) {
          // Advance the workflow to the next step or process
          workflowSession.advance(workItem, null);
          return;
        }
      }
    } catch (Exception e) {
      throw new WorkflowException("Error executing custom workflow process step", e);
    }
  }

  private Calendar getParticipantStepTime(WorkflowSession workflowSession, String workflowInstancePath, String participantId) throws WorkflowException {
    try {
      Node workflowInstanceNode = workflowSession.getSession().getNode(workflowInstancePath);
      if (workflowInstanceNode.hasNode("history")) {
        Node historyNode = workflowInstanceNode.getNode("history");
        NodeIterator iterator = historyNode.getNodes();
        while (iterator.hasNext()) {
          Node historyEntryNode = iterator.nextNode();
          String userId = historyEntryNode.getProperty("userId").getString();
          if (participantId.equals(userId)) {
            Calendar stepTime = historyEntryNode.getProperty("time").getDate();
            return stepTime;
          }
        }
      }
    } catch (Exception e) {
      throw new WorkflowException("Error retrieving participant step time", e);
    }
    return null;
  }
}

 

Avatar

Level 4

@Tanika02 , Thanks Tanika for the answer. I should set this as the TimeoutHandler, right?

Avatar

Community Advisor

Hello @rakesh_h2 - 

 

  • Yes, you can use the "Timeout Handler" configuration in the participant step to achieve a similar outcome.
  • By setting the "Timeout Handler" to "Absolute Time Auto Advancer" and specifying a value for the "Timeout" property, you can automatically advance the workflow if the participant hasn't acted within the specified time.

Here is how you can actually leverage Timeout Handler to auto advance the workflow to the next step if the user has not acted in a given timeframe : 

  1. Go to the workflow model.

  2. Locate the participant step for which you want to set the timeout.

  3. Edit the properties of the participant step.

  4. Set the "Timeout Handler" property to "Absolute Time Auto Advancer".

  5. Set the "Timeout" property to the desired value, specifying the duration after which the timeout should occur. This value is typically in milliseconds.

  6. Save the changes to the workflow model.

 

That's it!

Avatar

Correct answer by
Community Advisor

Hello @rakesh_h2 

 

You can use the "Timeout Handler" configuration in participant step to achieve the same.

 

Set "Timeout Handler" = "Absolute Time Auto Advancer". And set "Timeout" to some value

 

https://experienceleague.adobe.com/docs/experience-manager-65/developing/extending-aem/extending-wor...


Aanchal Sikka