Expand my Community achievements bar.

SOLVED

Unable to use WorkflowSession in Scheduler/Schedule Workflow Email Reminder

Avatar

Level 4

I am writing a scheduler to send an email reminder to approvers in a workflow that has done nothing in 7 days. However, I have no way to grab workflow data because the WorkflowSession that I create returns nothing when using its methods. I know for a fact that there are workflows running on my local instance, so its not that there isn't any active.

 

Example Code:

 

 

private static final String SUBSERVICE_NAME = "workflowServiceUser";

private static final Map<String, Object> authInfo = ImmutableMap.of(ResourceResolverFactory.SUBSERVICE, SUBSERVICE_NAME);

@Reference
private ResourceResolverFactory resolverFactory;

@Reference
WorkflowService workflowService;

ResourceResolver resolver = resolverFactory.getServiceResourceResolver(authInfo);
Session session = resolver.adaptTo(Session.class);
WorkflowSession wfSession = workflowService.getWorkflowSession(session);

//EXAMPLES
//wfSession.getActiveWorkItems(); -Returns 0 results
//wfSession.getAllWorkflows(); -Returns 0 results
//wfSession.getModels(); -for some reason this WORKS

 

 

After I determine which workitems are a week old, I need to essentially run our email workflow step again, and the only way to do that is to use the WorkflowSession.

 

Is there something that I need to be doing differently?

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hello @rkody 

The way I had implemented scheduling in a workflow is,

1. Implement a class that extends WorkflowProcess,

2. In that class, get metadataMap using workItem.getWorkflow().getWorkflowData().getMetadataMap();

3. Setting "absoluteTime" property in the metadatamap in milli seconds. This absoluteTime is a a future time.

4. The workflow step right after the above Process step has the "Timeout Handler" field set to "Absolute Time Auto Advancer". So, this step waits until the value in absoluteTime property is reached.

 

 

Hope this helps!

 

regards,

PreetpalSingh

View solution in original post

8 Replies

Avatar

Community Advisor

According to the documentation, the method getAllWorkflows() returns all Workflows which the WorkflowSession has access to.

Therefore, ensure that the Service User has the appropriate permissions to access the specific workflows you intend to retrieve.

The same condition is applicable to the getActiveWorkItems method as well.

Avatar

Level 4

I thought about it being a permission issue however, I could not find what permissions are needed. Would you know what permissions are required to access the workflows?

Avatar

Community Advisor

You might need Read permission to access the workflows. @rkody 

Avatar

Community Advisor

Hi @rkody 

Drawbacks of using scheduler

1. the time period for the each reminder will not be exact 24 hours or any predefined hours, untill unless you start the sling jobs at that exact time.

2. you need to filter the result set of the active workflows using the APIs.

 

IMO, instead of writing the scheduler to find the list of the users who have not taken the action on the activity assigned on them, it would be nice to handle it via the main workflow itself using the timeout handler.

 

This way via the timeout handler you get the exact duration of the time period the job was lying with the participant user.

 

Sample model 

Shailesh_Bassi__0-1693466163651.png

 

Timeout Settings

Shailesh_Bassi__1-1693466212049.png

Hope this helps!

Thanks

 

Avatar

Level 4

Through my testing this does not work. I have a timeout set on the participant step like you have suggested and an OR split to handle the advance. However when it advances and goes back to the participant, the timeout is instant which creates an infinite loop of the workflow advancing and then sending back to the participant. Do I have to somehow reset the timeout as well?

Avatar

Correct answer by
Community Advisor

Hello @rkody 

The way I had implemented scheduling in a workflow is,

1. Implement a class that extends WorkflowProcess,

2. In that class, get metadataMap using workItem.getWorkflow().getWorkflowData().getMetadataMap();

3. Setting "absoluteTime" property in the metadatamap in milli seconds. This absoluteTime is a a future time.

4. The workflow step right after the above Process step has the "Timeout Handler" field set to "Absolute Time Auto Advancer". So, this step waits until the value in absoluteTime property is reached.

 

 

Hope this helps!

 

regards,

PreetpalSingh

Avatar

Level 4

This worked for me. To make this answer more clear, I will provide the code and settings related.

 

1. I have a custom class that extends WorkflowProcess:

public class CustomProcess implements WorkflowProcess {
...
}

 

 2 and 3. I have these lines of code in that class to assign the "absoluteTime" variable:

Date date = new Date();
long millisecondTimeInSevenDays = date.getTime() + (60000 * 60 * 24 * 7); //millisecond value of 7 days. Customize to how long you wait to send a reminder
workItem.getWorkflowData().getMetaDataMap().put("absoluteTime", millisecondTimeInSevenDays);

 

4. In the workflow model editor, I have a step that assigns a timeout handler to "Absolute Time Auto Advancer"

TimeoutSettings.png

 

In addition, you will need to do what @ShaileshBassi did with the Or Split to handle the email notification.

Avatar

Administrator

@rkody We hope you found the AEM community valuable. We anticipate your return as either a learner or a contributor. The community benefits from SMEs like you. Please ask your AEM peers to join and contribute. Happy AEM learning!



Kautuk Sahni