Unable to use WorkflowSession in Scheduler/Schedule Workflow Email Reminder | Community
Skip to main content
rkody
Level 3
August 30, 2023
Solved

Unable to use WorkflowSession in Scheduler/Schedule Workflow Email Reminder

  • August 30, 2023
  • 2 replies
  • 1959 views

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?

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Preetpal_Bindra

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

2 replies

Mahedi_Sabuj
Community Advisor
Community Advisor
August 30, 2023

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.

Mahedi Sabuj
rkody
rkodyAuthor
Level 3
August 31, 2023

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?

Mahedi_Sabuj
Community Advisor
Community Advisor
September 1, 2023

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

Mahedi Sabuj
ShaileshBassi
Community Advisor
Community Advisor
August 31, 2023

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 

 

Timeout Settings

Hope this helps!

Thanks

 

rkody
rkodyAuthor
Level 3
August 31, 2023

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?

Preetpal_Bindra
Community Advisor
Preetpal_BindraCommunity AdvisorAccepted solution
Community Advisor
August 31, 2023

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