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

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 vote

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

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("")
}

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".

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"

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?

"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"

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

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

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

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

361 views 0 6
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you