How can I make a field read-only based on contents of another field?

I'd like to make a field readonly based on the contents of another custom field. Is that possible?

3 answers

Hi Alex,

We have a couple different ways of doing this, depending on what you want the read-only field dependent on. 

This article explains how use Behaviours to make a field read-only based dependent on user role or group. 

You could also use a Scripted Condition to do this. 

Let us know if those help you to achieve your desired result. 

Kind regards,

Katy

Adaptavist Product Support 

 

I provided my script in answer to you.

Can you provide a working example?

The field-dependency is just a person field (but not related to the current user) so can't use that article.

 

I did try a scripted condition, but am running into this (I think): https://productsupport.adaptavist.com/browse/SRJIRA-643

 

Can you confirm that bug is a real thing, the ETA for fixing it?

Hi Alex,

Can you post the script you are using and your versions of JIRA/ScriptRunner? 

ScriptRunner: 5.0.4

JIRA: 7.3.8

JSD: 3.5.2

 

Script:

import com.atlassian.jira.component.ComponentAccessor

def descField = getFieldByName("Description")

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def field = customFieldManager.getCustomFieldObjectByName("Field")
def value = getCustomFieldValue(field) as String

if ( value ) {
descField.setReadOnly(true)
}
else {
descField.setReadOnly(false)
}

 

Hi Alex. 

You shuold be using the behaviours module of SR. In that module, you will see that you don't need to use the customFieldManager.

Setup a behaviour for the project that you want this to affect, as we show you in the tutorial in the link above. When you edit your serverside Script, you will see that you can simply call, for example:

getFieldByName("Field").getValue() 

 If I were to adapt your script to a beaviour serverside script, I would do something like this:

def descField = getFieldById("description")

// You can also user getFieldById
// if you find the id of your custom field
def field = getFieldByName("Field")

def value = field.getValue()

if ( value ) {
descField.setReadOnly(true)
}
else {
descField.setReadOnly(false)
}

I would strongly advice that you take some time in running and understanding the examples that Katy has provided you in the documentation, since going through these will help you understand how to craft these scripts faster.

Please do tell me if you need anymore help. 

DYelamos

This doesn't appear to be working. It looks like it can't ready the value of the custom field correctly.

To test this out I've done the following:

def desc = getFieldById("description")
def field = getFieldByName("Field Name").getValue() as String
desc.setHelpText("Custom field should be:" + field)

The rest of the logic seems to be working - ie. as long as I'm not writing code that's dependent on that field, it's fine.

Actually, the issue seems to be in the getValue() call.

 

def field = getFieldByName("FieldName")
def value = field.getValue()
def descField = getFieldById("description")
descField.setHelpText("Field Details: " + field + ", Field Value: " + value)

 

Then, I get this in the helptext:

Field Details: Form field ID: customfield_11250, value: null, Field Value: null

That's the correct custom field ID, but the field does have contents - I can see it on the form.

So, without it being about to pull the value, then I can't write logic against it.

Per your examples here: https://scriptrunner.adaptavist.com/latest/jira/recipes/behaviours/scripted-conditions.html

I can access custom field contents with the JIRA API methods, but I can't use your methods (eg. setHelpText) on a field object returned from a JIRA standard call.

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

355 views 0 6
Read article

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