While configuring workflows in Polarion, we may want to have certain conditions fulfilled, before a work item or document can go into the next status. As long as we just want to configure, that some fields must be filled or a user must have a certain role, it’s easy. Complex conditions on the other hand must be scripted.

I will show in this post, how to prevent a work item from getting into approved status, as long as nobody else as the author has approved the work item. This will prevent, that the author can simply approve his own work items and put them into approved state, without anyone else approving it before.

I will directly „deep“ dive into writing the condition and show then, how you can configure it in Polarion.

You have basically three technical possibilities to create workflow conditions:

  • Java
  • Javascript
  • Groovy

While Javascript and Groovy are basically the same: scripts that you can deploy during runtime, the Java conditions have to be handled like extensions. This means: deleting the folder „/data/workspace/.config“ and restart of Polarion service. But on the other hand it’s much nicer, as you can really debug your conditions with an IDE. So for more complex things – Java conditions might be the choice. I’ll show it one day in another post.

Write Javascript Condition

But lets start with the small things first: Javascript conditions.
The scripts must be in the folder: „[InstallDir]/scripts“ e.g. „C:\Polarion\scripts“.
It must be a file with .js ending. To edit them, I use the Notepad++, it has by default a nice syntax highlighting for Javascript available.

So create a file called „condition_approvals_notAuthor.js“ in the scripts folder.
(As always: try this locally first or on a specific development system)

The principle in writing these conditions is very simple. At the end the „return_value“ must return true, otherwise the workflow won’t be allowed. In Polarion the workflow is then grey and can’t be selected.

Workflow blocked by script condition

So we need atleast one variable „return_value“, which we will set on false, as long as the condition is not met. As entry point the script conditions offer the so called „workflowContext“ variable. It’s an object from class ICallContext (See: https://almdemo.polarion.com/polarion/sdk/doc/javadoc/com/polarion/alm/tracker/workflow/ICallContext.html#getTarget())

It has the Method „getTarget()“ which will give you the targeted work item of this condition.

So the start looks like that:

var return_value = false;

var work_item = workflowContext.getTarget();

To implement our logic we also need the approvals and authors:

var return_value = false;

var work_item = workflowContext.getTarget();

var approvals = work_item.getApprovals();

var author = work_item.getAuthor();

Now we can start implementing the logic. We’ll set the return_value on true, if there is at least one approver, who isn’t the author:

var return_value = false;

var work_item = workflowContext.getTarget();

var approvals = work_item.getApprovals();

var author = work_item.getAuthor();

for (var i = 0; i < approvals.length; i++) {
	
	if (author.id != approvals.get(i).user.id && approvals.get(i).status.id === "approved") {
		
		return_value = true;	
		
	}

}

return_value;

In the end we simply paste the return_value. If the return value has any other string it will show this string as mouse over in the workflow action.

Configure your Workflow

Now that we have the script condition, we have to configure it in the administration.

Therefore we go to: Project Administration –> Work Items –> Workflow.

Worfklows

To add workflow conditions, we have to click on edit behind the workflow action we want to have the condition.

Workflow Actions

There we have to select under „Condition“ the „ScriptCondition“.

Set Condition

But this Condition must be configured now, so that Polarion knows which engine (js) to use and how the name of the script is. See more details in the standard Polarion help in the chapter „Scripted Workflow Functions and Conditions“.
To edit the parameters we have to click the edit button behind Script Condition:

Edit Parameters

Here we have to know how the parameters are called. It also allows us to make the conditions generic, by setting additional arguments in the Polarion configuration. The must have parameters are stated in the Help. They are called „engine“ and „script“.

Parameters of Script Condition

Click „Close“, but don’t forget to save the whole workflow now!

The workflow is now configured.

Advanced: Create your individual workflow condition with Javascript

Beitragsnavigation


2 Gedanken zu „Advanced: Create your individual workflow condition with Javascript

    1. Hi Sheetal,

      this is a very specific thing, but our goal is to provide the basics for everyone to accomplish something like that on their own.
      This is why I don’t think that we will be creating any blog post for this specific use case anytime soon.

      Best,
      PolarionDude

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert