Make field "Read-Only" on create screen based on the other field value (Condition)

Lakshmi S
Contributor
October 24, 2024

Hi Team,

I want to make a field "Read-Only" based on the value of another field. However, the field is completely disabled for other values during creation. Please review the behavior below and let me know if I've missed anything.

The fields are : Need By Date, Requester Dept.

The "Need By Date" should be read-only upon creation when the "Requester Dept." has specific values. I have added this behavior using the "Requester Dept." field.

Please check the attached screenshots for your refence.

b1.pngb2.png

 

import com.onresolve.jira.groovy.user.FieldBehaviours

import groovy.transform.BaseScript

 

@BaseScript FieldBehaviours fieldBehaviours

def dateField = getFieldByName("Need By Date")

def rdField = getFieldByName("Requester Dept.")

def rdValue = rdField.getValue()

if ((getActionName() in ["Create Issue", "Create"]) && (rdValue == "APAC Fulfillment Implementation" || "APAC Support" || "APAC System Automation Implementation"))

{

    dateField.setReadOnly(true)

     

}

else

{

    dateField.setReadOnly(false)

   

}

3 answers

1 accepted

0 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 26, 2024

Hi @Lakshmi S

For your Behaviour to work as expected, you need to change your code to something like this:-

import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours behaviours
def sampleList = getFieldById(fieldChanged)
def sampleListValue = sampleList.value.toString()

def sampleDate = getFieldByName('Sample Date')
sampleDate.readOnly = false

if (!underlyingIssue && sampleListValue == 'Option1') {
sampleDate.readOnly = true
}

Please note that the sample working code above is not 100% exact to your environment. Hence you must make the required modifications.

Below is a screenshot of the Behaviour configuration:-

behaviour_config.png

Whit the Server-Side Behaviour configuration above, when a new Issue is being created, if Option1 is selected from the Sample List, as expected, the Date field will be set to read-only. If any other option is selected from the list, as expected the Date Field will be editable.

I hope this helps to solve your question. :-)

I am looking forward to your feedback.

Thank you and Kind regards,
Ram

0 votes
Lakshmi S
Contributor
October 25, 2024

I was able to set the field "read only" based on the condition on create screen.


 

import com.onresolve.jira.groovy.user.FieldBehaviours

import groovy.transform.BaseScript

@BaseScript FieldBehaviours behaviours

def dateField = getFieldByName("Need By Date")

def rdField = getFieldById(fieldChanged)

def rdValue = rdField.value

if ((getActionName() in ["Create Issue", "Create"]) && rdValue == "APAC Fulfillment Implementation")

{

    dateField.setReadOnly(true)

   

}

else if ((getActionName() in ["Create Issue", "Create"]) && rdValue == "APAC Support")

{

    dateField.setReadOnly(true)

   

}

else if ((getActionName() in ["Create Issue", "Create"]) && rdValue == "APAC System Automation Implementation")

{

    dateField.setReadOnly(true)

   

}

else

{

    dateField.setReadOnly(false)

   

}
 

 

0 votes
Radek Dostál
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.
October 25, 2024

This part

rdValue == "APAC Fulfillment Implementation" || "APAC Support" || "APAC System Automation Implementation"

 

Will always evaluate true. Because groovy will just see "APAC Support" and that is a true-ish evaluation for it, it does not do anything with rdValue with it.

It should be either rdValue == in each or block; or you can use the same logic as you did previously with action name using rdValue in [...]

 

Suggest an answer

Log in or Sign up to answer