Expandir minha barra de realizações na Comunidade.

Get ready! An upgraded Experience League Community experience is coming in January.

Custom workflow to update another running workflow

Avatar

Level 1

Hi All,
I have a requirement as below:

There are 2 workflows:
1: The primary workflow which is doing some page related jcr modifications and this has a custom process step to add absoluetimeInMillSecons for autoAdvanceHandler thus if the author triggers the workflow with a particular time, the last step of the workflow is executed at that scheduled time.
2: Secondary workflow to modify the primary workflow's scheduled time. 
Here I'm getting the active workflows through the primary workflow's model ID and then getting the metadata of the workflow and modified the absoluteTime property in the metadata.
This is where I'm facing the issue. 
Right now, the code is like this:

public Workflow getCurrentWorkflowFromModel(WorkflowSession wfSession, String workflowModel) throws
WorkflowException {

Workflow[] runningWorkflows = wfSession.getWorkflows(workFlowStates); // Gets all running workflows
if(runningWorkflows != null){
LOGGER.error("Fetched all workflows from session");
LOGGER.error("Workflow to be fetched for the model {}", workflowModel);
for (Workflow workflow : runningWorkflows) {
LOGGER.error("Current Workflow Model: {}", workflow.getWorkflowModel());
if (workflow.getWorkflowModel().getId().equals(workflowModel)) {
LOGGER.error("Fetched workflow of the model:: {}", workflowModel);
return workflow;
}
}
}else{
LOGGER.error("Could not fetch the active workflows");
}
return null;
}
Workflow activeWorkflow = getCurrentWorkflowFromPayload(wfSession, payloadPath);
activeWorkflow.getWorkflowData().getMetaDataMap().put("absoluteTime", scheduledtime.getTimeInMillis());

 But the line "

activeWorkflow.getWorkflowData().getMetaDataMap().put("absoluteTime", scheduledtime.getTimeInMillis());

is not working as I expected as jcr is not persisting the metadata property value set here. 

I would like to know if there's any other approach to achieve this. 
Any guidance will be much helpful.
Note: I'm working with AEM as a cloud service

Thanks in advance,
Dayana

 




Tópicos

Os tópicos ajudam a categorizar o conteúdo da comunidade e aumentam sua capacidade de descobrir conteúdo relevante.

2 Respostas

Avatar

Community Advisor

Hi @Dayana_Ramesh12,

Use WorkflowSession.updateWorkflowData()

Adobe recommends that for modifying workflow metadata at runtime, you must update it via the official API:

WorkflowData wfData = activeWorkflow.getWorkflowData();
wfData.getMetaDataMap().put("absoluteTime", scheduledtime.getTimeInMillis());

// persist the updated workflow data
wfSession.updateWorkflowData(activeWorkflow, wfData);
  • This forces AEM to rewrite the workflow instance
  • Metadata is updated under /var/workflow/.../metadata
  • Works in AEM as a Cloud Service

You cannot modify an already completed step

If your “primary workflow” already passed the process step that reads absoluteTime,
changing the metadata later will not reschedule the step.

For scheduled execution, the AutoAdvanceHandler reads absoluteTime only when the step reaches it.

So the secondary workflow must modify the workflow before that step is reached.

Recommended Implementation Pattern

Step 1: Locate running workflow instance

(Your current logic is fine)

Step 2: Modify metadata + persist it

Workflow activeWorkflow = getCurrentWorkflowFromModel(wfSession, modelId);

if (activeWorkflow != null) {
    WorkflowData wfData = activeWorkflow.getWorkflowData();

    wfData.getMetaDataMap().put("absoluteTime", scheduledtime.getTimeInMillis());

    // THIS SAVES THE CHANGE
    wfSession.updateWorkflowData(activeWorkflow, wfData);

    LOGGER.info("Updated absoluteTime on workflow instance {}", activeWorkflow.getId());
} 

 


Santosh Sai

AEM BlogsLinkedIn


Avatar

Level 2

Hi @Dayana_Ramesh12 ,

The issue you're facing is because updating the metadata using getMetaDataMap().put() only modifies the data in memory. It doesn’t automatically persist to the workflow instance in JCR.
To make the updated absoluteTime value actually save, you need to update the workflow instance through the session. 

You can do it like this:

Workflow activeWorkflow = getCurrentWorkflowFromModel(wfSession, modelId);

if (activeWorkflow != null) {
    WorkflowData wfData = activeWorkflow.getWorkflowData();
    wfData.getMetaDataMap().put("absoluteTime", scheduledTime.getTimeInMillis());

    // persist metadata to workflow instance
    wfSession.updateWorkflowData(activeWorkflow, wfData);
}

 

Just a couple of things to keep in mind as you implement this:

1.This update works only if the workflow hasn’t yet passed the step that reads absoluteTime. Once it crosses that step, updating the metadata won’t impact scheduling.

2.If rescheduling might happen later too, you could consider storing the date/time externally (like under /var/...) and let your workflow read it dynamically instead of relying only on metadata.

3.If the step already executed, then rescheduling may require restarting the workflow or using a scheduler/OSGi job approach.

Hope this clarifies why it wasn’t persisting and helps you move ahead with your secondary workflow approach.