How to fetch the data of component using behaviour script runner

Nishant Paraskar November 9, 2020

As i used validation on component (to select/restrict  to   one field only )

 

------------------------------------------------------------------------------

1st i tried this 

import com.onresolve.jira.groovy.user.FieldBehaviours
import org.apache.log4j.Logger
import org.apache.log4j.Level
import groovy.transform.BaseScript
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.project.component.ProjectComponent

def component = getFieldById('components-textarea')
List <ProjectComponent> result = component.getFormValue() as List


if(result.size() >1)
{

component .setError("Only one Component is allowed")

}

else {
component.clearError()
}

 

 

------------------------------------------------------------------------------

 2 ] I tried  to get atleast size of selected element 

 

def c = ComponentAccessor.componentClassManager.getMetaPropertyValues()

 

def component = getFieldById('components-textarea')
component.setHelpText("----->>Multi Select Field is set to Support " + c.size())

 

 

------------------------------------------------------------------------------

Help me to put validation on component field  . My Requirement is to select only one field from System component field .

 

2 answers

1 accepted

2 votes
Answer accepted
Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 9, 2020

Hi @Nishant Paraskar ,

you are using incorrect id for components field, it should be "components" instead of "components-textarea".

Something like this should work:

import com.atlassian.jira.bc.project.component.ProjectComponent
import com.onresolve.jira.groovy.user.FormField
import static com.atlassian.jira.issue.IssueFieldConstants.COMPONENTS

FormField componentsField = getFieldById(COMPONENTS)
List<ProjectComponent> components = componentsField.getValue() as List

if (components.size() > 1) {
componentsField.setError("Only one Component is allowed")
} else {
componentsField.clearError()
}
Yana Stoliarova
Contributor
December 13, 2022

Excellent! Thanks

Alex
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.
February 27, 2024

@Hana Kučerová thank you! Your code helpfull!

Like Hana Kučerová likes this
arno
Contributor
June 27, 2024

Hi @Hana Kučerová 

The code you provided is very effective, I use this method to get the name of component, but when I get the comment, I find that the comment information is not valid, here is my code, when I comment the definition of variable name, the last three lines of code are valid, if I do not comment, the last three lines of code are not valid. Can you help me see why, thank you


import com.atlassian.jira.bc.project.component.ProjectComponent
import com.onresolve.jira.groovy.user.FormField
import static com.atlassian.jira.issue.IssueFieldConstants.COMPONENTS



FormField componentsField = getFieldById(COMPONENTS)
List<ProjectComponent> components = componentsField.getValue() as List
def name = components.getAt(0).name



def com = getFieldById('commentLevel')
def text = """ this is a text  """


com.setHelpText(text)
0 votes
Lee Wonnacott
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.
November 10, 2020

test

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events