How do I get the list of component/s when utilizing ScriptRunner?

Terrell December 9, 2019

My team wants to add a custom field that is read only until specific Components are selected. I have created a Behaviour on the field already. Currently the field is read only. I would like to create a custom script that can change the custom field to "writable" once the"Sys Admin" or "SysAdmin" component's are chosen. I've been banging my head on this for the last two days and I just feel lost now. Getting the list for the components seems to be the hardest part. Please help me out. (We have the server addition)

1 answer

1 accepted

0 votes
Answer accepted
Leo
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 9, 2019

Hi @Terrell 

I'm not a efficient coder, though below snippet will do the trick for you

import com.atlassian.jira.bc.project.component.ProjectComponent

def componentFF = getFieldById(fieldChanged)
def field = getFieldByName("Field name to be read-only")

field.setReadOnly(true)

List <ProjectComponent> components = componentFF.getValue() as List
def isThere = false

for(c in components){
if(c.getName() == "Sys Admin" || c.getName() == "SysAdmin"){
isThere = true
break
}
}
if(isThere){
field.setReadOnly(false)
}

 

Note: this behaviour should be added for Component/s field not for Custom field

 BR,

Leo

Terrell December 10, 2019

Hey Leo, thanks for taking the time to reach out... I've implemented the code example you suggested and I'm not getting any errors but I am also not getting any results. I have the code executing as a server-side script. I am expecting the issue to attempt submission, but fail because I have set the "sysAdminCF" field as required. Is the logic incorrect?

Also, I'm not sure if I am using the correct component id as there are multiple id's within the man Div.2019-12-10_11-11-38.png2019-12-10_11-27-54.png

Leo
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 10, 2019

I'm not sure whether "components-textarea" is the correct one or not

you can use

def component = getFieldById(fieldChanged)
Like Alex Sorikov likes this
Terrell December 10, 2019

Ahh yes! I wasn't sure if "fieldChanged" was just used as a placeholder for the id of the component field or what. That explains why I wasn't getting any results. Thanks for your help, it works like a charm!

Suggest an answer

Log in or Sign up to answer