How does WorkflowTransitionService work?

I have been learning how to create project-blueprint module and playing around with more advanced configurations like adding custom fields. I would also like to add postfunctions and conditions to the workflows - now in the basic template tutorial says under the part about AddProjectHook.java “The configure() function can be used for a wide variety of configuration options. For example, you can use this to create Workflow Post Functions, Resolutions, and more”, but it doesn’t specify on how exactly do you actually create the postfunctions and transitions.

I have searched around and discovered the WorkflowTransitionService class, which seems to be able to do exactly what I need since it has methods addConditionToWorkflow and addPostFunctionToWorkflow. I tried to use it this way:

WorkflowTransitionService workflowTransitionService = ComponentAccessor.getComponent(WorkflowTransitionService.class);

ConditionDescriptor conditionDescriptor = DescriptorFactory.getFactory().createConditionDescriptor();
conditionDescriptor.setType("class");
final Map conditionArgs = conditionDescriptor.getArgs();
conditionArgs.put("class.name", "cz.principal.jira.TestCondition");

workflowTransitionService.addConditionToWorkflow("In Progress", conditionDescriptor, basicWorkflow);

There are no errors returned in the ErrorCollection from the method, or exceptions of any kind, yet there is no condition attached to the transition when the project is created. I have discovered this question which is exactly the same problem as I am having, sadly unanswered.

Unfortunately no one ever replied to my previous post – Currently the only way I know to manage these functions is via the OpenSymphony classes.

Hmm that is very unfortunate. Could you provide me with a code snippet please?

This is quite specific to my use-case, but I tried to generify it a little for you.

JiraWorkflow workflow = workflowManager.createDraftWorkflow(authenticationContext.getLoggedInUser(), workflow.getName());

FunctionDescriptor postFunction = functionFactory.updateIssueField("resolution", null);
Collection<ActionDescriptor> actions = workflow.getActionsByName("Close");
for (ActionDescriptor action : actions) {
    List postfunctions = action.getUnconditionalResult().getPostFunctions();
    postfunctions.add(0, postFunction);
 }

workflowManager.saveWorkflowWithoutAudit(workflow);

Thanks to your code, some huge coincidence and a lot of Jira source code debugging I have been able to solve the original issue with the WorkflowTransitionService :slight_smile:
When I tried to add your code, there was an exception telling me, that there already exists a draft for this particular workflow - and that is the main problem we have been having. The service creates a draft (or takes an existing one) and edits that DRAFT, calling only WorkflowService.updateWorkflow at the end of the method, which literally says in the doc “This method does not save the changes to the active workflow. Instead, this is meant to be used to make persistent changes to your working edit.

So basically all we have to do is to just add this call to the original code to take the draft and replace the original workflow with it, and voila - it works like a charm.

workflowManager.overwriteActiveWorkflow(authenticationContext.getLoggedInUser(), basicWorkflow.getName());