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

Alex Wasserman July 12, 2017

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

3 answers

0 votes
Daniel Yelamos [Adaptavist]
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.
July 25, 2017

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

Alex Wasserman July 31, 2017

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.

Alex Wasserman July 31, 2017

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.

0 votes
Alex Wasserman July 13, 2017

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?

Katy Kelly
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.
July 13, 2017

Hi Alex,

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

Alex Wasserman July 13, 2017

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)
}

 

0 votes
Katy Kelly
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.
July 13, 2017

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 

 

Alex Wasserman July 24, 2017

I provided my script in answer to you.

Can you provide a working example?

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events