Jira Read Only field using Behaviors

Jon Kocen
Contributor
November 30, 2023

I tried using the Adaptavist script from their libary

I put this into a post function but only get an error can't operate on a null field for getFieldByName() 

Am I using this wrong, can it be used in a post function to make a field read only? I need to stay in the same status for business reasons.

 

import com.atlassian.jira.issue.IssueFieldConstants
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
 
@BaseScript FieldBehaviours fieldBehaviours
 
final String fieldName = "TextField"
 
// get field by name and make it read only
getFieldByName(fieldName).setReadOnly(true)
 
// get field by id and make it read only
getFieldById(IssueFieldConstants.DUE_DATE).setReadOnly(true)

2 answers

0 votes
Jon Kocen
Contributor
December 4, 2023

I think I figured this out. I ended up adding a Behavior with no options selected just some Server Side Script

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRoleManager

def projectManager = ComponentAccessor.projectManager
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)

// get user
def user = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()
// get project
def thisProject = issueContext.getProjectObject()
// get user's roles for this project
def projectRoles = projectRoleManager.getProjectRoles(user, thisProject)

def RI = false
def BP= false
// if RRI or BP, you can still edit
if (projectRoles.find(){it.getName() == "RI"}) { RI= true }
if (projectRoles.find(){it.getName() == "BP"}) { BP= true }

// get CT value
def CTValue = getFieldByName('CT').value
// get CoT
def CoT = getFieldByName("CoT")
// get CLV value
def CLV = getFieldByName("VLV").value

// set to read only if roles and field values match
if (CLV== 'Yes' && !RI && !BP) {
    // make read only
    CoT.setReadOnly(true)
}
else {
    CoT.setReadOnly(false)
}
0 votes
Jeroen Poismans
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 1, 2023

Hi Jon,

I don't think you can use these method (getFieldByName, getFieldById..) in postfunctions, only in behaviours. The context is different in both cases.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events