how to get component lead according to chosen component when using JIRA Behaviours Plugin

fabby May 15, 2012

my jira version is jira 4.4.1,Behaviours Plugin version:0.5.0

there are two fields:User and Component

what i want to implement is :

when select a component ,the field "User" will be set to the component lead's user name.

If you unselect the component or select Unknown it should be cleared.

i refer to the Behaviours Plugin document(https://studio.plugins.atlassian.com/wiki/display/JBHV/JIRA+Behaviours+Plugin#JIRABehavioursPlugin-Installation),and find the script follows,

FormField formComponent=getFieldById("components")
FormField formUserField=getFieldByName("customfield_11000")

List<ProjectComponent> components=formComponent.getValue() as List
if(components){
    formUserField.setFormValue(components.first().lead)
}else{
    formUserField.setFormValue("")
}

there are some compilation errors,

Compilation failure: startup failed: Script1.groovy: 6: unable to resolve class ProjectComponent @ line 6, column 6. List<ProjectComponent> components=formComponent.getValue() as List ^ 1 error

1 answer

1 accepted

0 votes
Answer accepted
JamieA
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.
May 15, 2012

You can either add the import statement for ProjectComponent, or easier, just remove the generic, ie remove <ProjectComponent>.

fabby May 15, 2012

i add the import statement,now it compile succeed ,but the field "User" is not changed yet(it is always "blank") ,no matter which component i choose .

the script is

import com.atlassian.jira.bc.project.component.ProjectComponent
FormField formComponent=getFieldById("components")
FormField formUserField=getFieldByName("customfield_11000")

List&lt;ProjectComponent&gt; components=formComponent.getValue() as List
if(components){
 formUserField.setFormValue(components.first().lead)
}else{
 formUserField.setFormValue("")
}

JamieA
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.
May 15, 2012

This line:

FormField formUserField=getFieldByName("customfield_11000")

should be:

FormField formUserField=getFieldById("customfield_11000")

by name means give the name, eg "Name of custom field".

fabby May 15, 2012

yes ,FormField formUserField=getFieldById("customfield_11000") is effective.

and FormField formUserField=getFieldByName("User") will get the same result.

but ,there is still one problem,when i have selected one component ,for example "component1" whose associated component leader is "Jim",and the field "User" will display as "Jim"

then i select Unknown,the "User" is not cleared,it still display as "Jim"

JamieA
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.
May 15, 2012

What's "Unknown"? That does not display in the components field, is it the name of a component you created?

I presume you've put this script on the components field?

fabby May 24, 2012

"Unknown" is one of Component's option value

&lt;select class="select " id="components" multiple="multiple" name="components" size="3" data-remove-null-options="true"&gt;
        &lt;option value="-1"&gt;
               Unknown
        &lt;/option&gt;
        &lt;option selected="selected" title="A " value="10101"&gt;
                A
         &lt;/option&gt;
         &lt;option title="B" value="10100"&gt;
                B
          &lt;/option&gt;
&lt;/select&gt;

when i select option A ,and view source code of the jira web page.and find that,

<option selected="selected" title="A" value="10101">

the attribute selected="selected" is added to option A.

then i select the option "Unknown" ,option A still has the attribute selected="selected" ,while option "Unknown" doesn't have the attribute

so ,i think this can explain why the field "User" still display as previous selected "A"'s value,although i have changed Component's value to "Unknow"

fabby May 24, 2012

now,i have understand most of this example's code

1, List<ProjectComponent> components=formComponent.getValue() as List

"formComponent.getValue()" returns Component objects,and "as List" means convert type to List("as" is groovy syntax)

2, formUserField.setFormValue(components.first().lead)

"components.first() " return the first item in the list form groovy syntax

"lead" returns the name of the lead for this project component,i searched the method without result,but

found getLead() method from JIRA API. i replace "lead" with "getLead()" ,and get the same result

Daniel Luevano August 12, 2015

with this line *formUserField.setFormValue(components.first().lead)* I got the userID, how can I get the Lead Name?

JamieA
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.
August 18, 2015

depends on your jira version, getComponentLead should give you an ApplicationUser in recent versions.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events