Script Runner Condition to Show Specific Screen

Hi,

I am using the script runner plugin to clone an issue to a specific project based on the value of a custom field.

By using a transition screen I can allow the user to enter new mandatory parameters required to create the issue in the new project. All statuses can transition to this status.

I can specify a transition screen in my workflow but this will mean the same screen will be shown all the time.

How can show different transition screens based on the value of my custom field? So that a new transition screen can be shown to create an issue for different projects.

4 answers

1 accepted

Accepted Answer
2 votes

You can use Behaviours to modify the visible/required fields on a screen. You can use a scripted transition to limit the behaviour to a particular workflow step, so that it's only applied on the relevant transition screen. You could add an initialization script to a behaviour with something like

if (getActionName() == "Transfer Ownership") {
    def fieldToHide = getFieldByName("Some custom field I wish to hide")
    def fieldToRequire = getFieldByName("Some custom field I wish to require")
    fieldToHide.setHidden(true)
    fieldToRequire.setRequired(true)
}

Notably, that would require you to add to the workflow transition screen. If you only wanted that field to show up for that particular transition (and no other transitions), you could do something like:

def fieldsThatOnlyShowOnSomeTransition = ["Field A", "Field B", "Field C"]
fieldsThatOnlyShowOnSomeTransition.each{ fieldName ->
    boolean includeFieldX = getActionName() == "Start Progress"
    def fieldX = getFieldByName(fieldName)
    fieldX.setHidden(includeFieldX)
}

Which can be a little more readable and maintainable for a large number of fields. It's also easier to adjust if the parameters that determine whether a given field should show are more complex than simply "Are we in some particular workflow step?"

Thanks. I am currently implementing this recommendation.

I have run into one stumbling block thus far.  In the Initializer function, I can get the object of one of my custom field that is of a Group Picker (Single) type using getFieldByName, but when i call getValue() on the object I get a null value.

 

The same function works fine if I add a field with a validation script. It seems like when the initializer script is executed the value is not yet populated.

Initializer scripts are always run as soon as the page is loaded, and are only run once. If that field is empty when the page is run, you should expect its value to be null when the initializer runs.

If you need a script to run every time a particular field changes, then it needs to be associated with that field. For some cases, you may need to examine a particular field in both an initializer and in a validation script on that field (or even on some other field). It just depends on what you're trying to do with that script.

That's not quite the case... no values are available in an initialiser whether set or not. You can get the value of any CF using the regular java api, getUnderlyingIssue() will give you the regular Issue object, then use issue.getCustomFieldValue

1 vote
Vasiliy Zverev Community Champion Jan 18, 2017

You could create several transitions with different transition screen. 

Add a condition on each to show for user only one at a time basedon custom field value.

We have over 20 statuses in this workflow with each status allowing all transitions from any other status enabled.

If we had to keep the same behaviour by creating individual transitions between each status, that would make the workflow really cluttered and messy.

Is there no other alternative ? Like calling a ShowScreen("ABC: Bug Screen") method from a condition ?

This is the problem I am trying to solve:

We have a project A that contains issues that Team A works on. Team A assigns these issues to various other teams using a custom field group picker, example team B.

Team B does not want to conform to project A's workflow and custom fields. They want to use their own workflow and custom fields.  

To solve this we are using the script runner clone issue post-function.  We check the value of the custom field and depending on the value we create the linked issue to the new project, example project B that team B works on.  Project B requires additional information when creating issues, Team A knows this information, so they need to fill these in when assigning the ticket to Team B.

Depending on the team that Team A assigns the issues to, the issues will need to be created on a new project showing different screens.

import com.onresolve.jira.groovy.user.FormField

FormField dropDown = getFieldById("customfield_10306")
FormField fconditionA = getFieldById("customfield_10510")
FormField conditionB = getFieldById("customfield_10515")

if (dropDownValue.getFormValue() == "High")

{ //Yes

conditionA.setHidden(false)
conditionB.setHidden(true)

}
else
{
f conditionA.setHidden(true)
conditionB.setHidden(false)
}

Good day,

 

I'm using the same code and nothing is happening

 

 

Below is my code and it's not doing anything.

 

import com.onresolve.jira.groovy.user.FormField

FormField dropDown = getFieldById("customfield_10306")
FormField fconditionA = getFieldById("customfield_10510")
FormField conditionB = getFieldById("customfield_10515")

if (dropDownValue.getFormValue() == "High")

conditionA.setHidden(false)
conditionB.setHidden(true)

}
else
{
f conditionA.setHidden(true)
conditionB.setHidden(false)
}

 

Jira Service Desk 3.6.1

ScriptRunner 5.1.6

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Sep 18, 2018 in Jira

What modern development practices are at the heart of how your team delivers software?

Hey Community mates! Claire here from the Software Product Marketing team. We all know software development changes rapidly, and it's often tough to keep up. But from our research, we've found the h...

21,699 views 2 7
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you