Make Fix Version/s readonly

Naveen Verma January 11, 2023

Hi - I am using Adaptavist for Jira.

On behaviors tab, In my listener, I have a situation where I want to make ''Fix Version/s" field read-only only for the given issue based on certain situation. I tried various things but can't seem to find any solution to it.

 

Any pointer would be appreciated.

3 answers

1 accepted

1 vote
Answer accepted
Naveen Verma January 13, 2023

Finally got it working.

I was trying to use Listener but in reality the script should be written on Behavior tab of 'Adaptavist for Jira'.

 

For others, this is how my script looks like.

 

import java.lang.Double

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueFieldConstants
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
import org.apache.log4j.Level

@BaseScript FieldBehaviours fieldBehaviours

log.setLevel(Level.DEBUG)

final targetCustomFieldName = 'My Custom Field Name'
def customFieldManager = ComponentAccessor.customFieldManager

def issueKey = underlyingIssue.key
log.info("From Behaviour - issueKey=="+issueKey)

def issue = ComponentAccessor.issueManager.getIssueByCurrentKey(issueKey)

def targetCustomField = customFieldManager.getCustomFieldObjects(issue).find {it.getFieldName() == targetCustomFieldName && it.getCustomFieldType().getName() == "Number Field"}
log.info("Found targetCustomField to be updated as $targetCustomField");
def targetFieldValue = issue.getCustomFieldValue(targetCustomField) as Double ?: 0
log.info("From Behaviour - targetFieldValue="+targetFieldValue)

def otherCustomField = customFieldManager.getCustomFieldObjects(issue).find { it.getFieldName() == "My Other Custom Field"}
log.info("From Behaviour - otherCustomField="+otherCustomField)
def otherCustomFieldValue = issue.getCustomFieldValue(otherCustomField)
log.info("From Behaviour - otherCustomFieldValue="+otherCustomFieldValue)


if (targetFieldValue >= getOtherCustomFieldValue(otherCustomFieldValue)) {
getFieldById(IssueFieldConstants.FIX_FOR_VERSIONS).setReadOnly(true)
log.info("From Behaviour, FIX_FOR_VERSIONS is non-editable")
} else {
getFieldById(IssueFieldConstants.FIX_FOR_VERSIONS).setReadOnly(false)
log.info("From Behaviour, FIX_FOR_VERSIONS is editable")
}


private Integer getOtherCustomFieldValue(otherCustomFieldValue) {
return 10 // OR whatever
}

 

0 votes
Naveen Verma January 13, 2023

Can anybody guide me on this, please?

0 votes
Ste Wright
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 11, 2023

Hi @Naveen Verma 

What's the specific situation you want to make FixVersions read only?

Ste

Naveen Verma January 12, 2023

I have a custom field counter that I increment every time a change is made in 'Fix Versions/s'  field. However, if that counter has reached a threshold then I want to make 'Fix Version/s' read-only/disabled only for that Issue.

'Fix Version/s' seems to be system field. If I try to grab it using code like below but getting exception.

import com.atlassian.jira.issue.IssueFieldConstants
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

final String fieldName = "Fix Version"
getFieldByName(fieldName).setReadOnly(true)
throws this exception
java.lang.NullPointerException: Cannot invoke method getFieldIdByName() on null object at com.onresolve.jira.groovy.user.FieldBehaviours.getFieldByName(FieldBehaviours.groovy:

 

I also tried this code which executes successfully but never makes the Fix Version read-only.

        def fixVersionsField = getFieldById("fixVersions")

        log.info("fixVersionsField="+fixVersionsField)

        fixVersionsField.setReadOnly(true)
 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events