Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to make a Field Required Based Off Value of Another Field

Elif Alverson
Contributor
May 30, 2018

Hello, 

Is there a way to make custom field called " ECC Reason" required if the Label is selected as " Emergency" ? 

If this can only be done by a script runner , do you have any samples?

Thank you.

We use JIRA 7.5.0

Elif 

3 answers

0 votes
Mark Markov
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 8, 2018

Hello @Elif Alverson

If your quiestion actual.

Yes, as David say, you need behaviour mapped to the labels field.

But labels field is special one. When there is only one label it is String object, but if there two or more its List<String> object, so keep it in mind, we can make script like this:

 

def labels = getFieldById(getFieldChanged())
def targetField = getFieldByName("ECC Justification")
targetField.setRequired(false)
if ("EmergencyChange".equals(labels.getValue()) || labels.getValue().find {it == "EmergencyChange"} ){
targetField.setRequired(true)
}

I check it on ScriptRunner v5.4.7, it works! Hope it help you!

Elif Alverson
Contributor
June 11, 2018 edited

@Mark Markov

Thank you for your response. It works on transition step " Submit for Approval". 

We need " ECC Justification" to be required only for transition " Resolved".

I put the conditions however it does  not work.

Any ideas? 

Thank you.

 image.png

 

Mark Markov
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 12, 2018

Hello @Elif Alverson

You can use method getActionName() to find current transition, see code bellow

if ("Resolved".equals(getActionName())) {
def labels = getFieldById(getFieldChanged())
def targetField = getFieldByName("ECC Justification")
targetField.setRequired(false)
if ("EmergencyChange".equals(labels.getValue()) || labels.getValue().find { it == "EmergencyChange" }) {
targetField.setRequired(true)
}
}
Elif Alverson
Contributor
June 12, 2018

@Mark Markov, I have tried however it does not work. I can resolve issues with EmergencyChange labels without filling " ECC Justification" custom field. 

Any idea why?

image.png

Mark Markov
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 12, 2018

getActionName() contains transition name, in your case it seems transition have name "Resolve Issue", so this name must be in code:

if ("Resolve Issue".equals(getActionName())) {
def labels = getFieldById(getFieldChanged())
def targetField = getFieldByName("ECC Justification")
targetField.setRequired(false)
if ("EmergencyChange".equals(labels.getValue()) || labels.getValue().find { it == "EmergencyChange" }) {
targetField.setRequired(true)
}
}
Elif Alverson
Contributor
June 12, 2018

Tried it, still no change. " ECC Justification" is not required.

See below.image.png

Mark Markov
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 12, 2018

Remove conditions above. Leave only the script.

Elif Alverson
Contributor
June 12, 2018

Same result nothing changed.

image.png

Mark Markov
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 12, 2018

I check it on my local environment and it work perfectly, are you sure that transition name is correct in script? Try to add log.error() after condition and see logs, to make sure that behaviour runs correctly on screen we need

if ("Resolve Issue".equals(getActionName())) {
log.error("Behaviour start on screen Resolve issue")
def labels = getFieldById(getFieldChanged())
def targetField = getFieldByName("ECC Justification")
targetField.setRequired(false)
if ("EmergencyChange".equals(labels.getValue()) || labels.getValue().find { it == "EmergencyChange" }) {
targetField.setRequired(true)
}
}
Elif Alverson
Contributor
June 12, 2018

Here are the workflow steps below. So when you send the ticket to " resolve issue" , the edit screens pops up saying the " ECC Justification" needs to be filled? How does it work?

We use v7.5.0, does it make any difference?

I do not have access to the logs.

image.png

Mark Markov
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 18, 2018

Do you have some others behaviours? 

If there are two or more behaviours executing on the screen and one of them fails, all behaviours fails.

Elif Alverson
Contributor
June 25, 2018 edited

@Mark Markov

 

I also have this behavior for the same issue type and it works just fine.image.png

0 votes
David STOCKY June 7, 2018

Scriptrunner behaviours:

 

def dropDown = getFieldByName("Liste1")
def hiddenField = getFieldByName("Liste2")
log.debug("dropdown value" + dropDown.getValue())

hiddenField.setRequired(false)
if (dropDown.getValue() == "aa") {
     hiddenField.setRequired(true)
}

Elif Alverson
Contributor
June 7, 2018 edited

@David STOCKY,

Here is what I am trying to do. If Jira user states " Labels= EmergencyChange", then the custom field " ECC Justification" is required. 

I tried the script you have provided but nothing happens. What am I doing wrong? Thanks!

Here it is;image.png

 

 

David STOCKY June 7, 2018

You are running a script on ECC Justification.

Or you want to have a special behaviours on the change on the fields "Labels".

So you have to move your script from ECC Justification (Fields) to Labels (Fields) on your script attached.

Do you get the point?

You script should be launch on Labels's event

Elif Alverson
Contributor
June 7, 2018

Here what I have tried no change :(

image.png

David STOCKY June 7, 2018

Try to log information:

import org.apache.log4j.Logger
import org.apache.log4j.Level

def log = Logger.getLogger("com.behaviours")
log.setLevel(Level.DEBUG)

log.debug("text to display on log")

 

Make sure you can get our field. Maybe your can try getFieldById instead of getFieldByName

David STOCKY June 7, 2018

Did you set the correct mapping of this behaviours? Projet / Issue Type?

Elif Alverson
Contributor
June 7, 2018 edited

@David STOCKY

where to put this one to see the output;

import org.apache.log4j.Logger
import org.apache.log4j.Level

def log = Logger.getLogger("com.behaviours")
log.setLevel(Level.DEBUG)

log.debug("text to display on log")

 

Yes, I did set it to correct Project/Issue Type.

David STOCKY June 7, 2018

something like that directly on your script and log will appear on jira server log 

------------------------------------------------

import org.apache.log4j.Logger
import org.apache.log4j.Level

def log = Logger.getLogger("com.behaviours")
log.setLevel(Level.DEBUG)

log.debug("start script")

def dropDown = getFieldByName("Labels")
def requiredField = getFieldByName("ECC Justification")
log.debug("dropdown value" + dropDown.getValue())

log.debug("set required false")

requiredField.setRequired(false)
if (dropDown.getValue() == "EmergencyChange") {

    log.debug("set required true")
    requiredField.setRequired(true)
}

log.debug("stop script")

Elif Alverson
Contributor
June 7, 2018

@David STOCKY,  I will not able to share the logs with you. 

Thanks for your time. I need to find another way to make this work.

0 votes
Scott Theus
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 30, 2018

Hi Elif,

There are a few examples in the Community, here is one:

https://community.atlassian.com/t5/Jira-questions/Required-field-and-condition/qaq-p/752807

Hope this helps,

-Scott

Suggest an answer

Log in or Sign up to answer