It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Adding multiple components using scriptrunner cloud Edited

Hi, I've just migrated from scriptrunner server to scriptrunner cloud and am trying to re-write my script to add multiple components to an issue.

I've tried this:

issueInput.fields.components = [[name: 'Operations1']]

which works and just sets a single component.

But when I try this:

issueInput.fields.components += [[name: 'Operations1']]

issueInput.fields.components += [[name: 'Operations2']]

I get the following error: 

2019-04-16 10:25:42.463 ERROR - Cannot execute null+[{name=Operations1}] on line 1
2019-04-16 10:25:42.503 ERROR - Class: com.adaptavist.sr.cloud.workflow.UpdateIssue, Config: [className:com.adaptavist.sr.cloud.workflow.UpdateIssue, uuid:3f47bc8a-64d6-4100-840b-77be047f1861, description:Add Operations1 and Operations2 as Components, enabled:true, executionUser:ADD_ON, additionalCode:issueInput.fields.components += [[name: 'Operations1']]
issueInput.fields.components += [[name: 'Operations2']], userKey:e01807, accountId:5cb5998a9a857910896fd759]

 Any ideas how I can get this to work please? I need the script to add the new components and leaving alone any existing components already attached to the issue. i.e. I do not want to overwrite what is there already - I need the new ones to be added.

2 answers

Hi @Tim Willmott ,

Not sure, but I suppose You need do it that:

def array = []

array.add([name: 'A'])

array.add([name: 'B'])

...

issueInput.fields.components = array

Or 

issueInput.fields.components.add([name: 'A'])

 I will check on my Jira and will write.

Hi @Tim Willmott ,

Below work for me:



import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.project.component.ProjectComponent

def projectComponentManager = ComponentAccessor.getProjectComponentManager()


def coll = projectComponentManager.findByComponentNameCaseInSensitive('A')
coll.addAll(projectComponentManager.findByComponentNameCaseInSensitive('B'))


issue.setComponent(coll)

 More You might read here https://community.atlassian.com/t5/Jira-Core-questions/Script-runner-for-setting-up-a-component-based-on-issue-type/qaq-p/841359

That script is for Scriptrunner server version - not the cloud version. I'm using the cloud version. Your script gets the following errors on cloud:

2019-04-16 20:02:30.267 ERROR - startup failed:
Script1.groovy: 2: unable to resolve class com.atlassian.jira.issue.MutableIssue
 @ line 2, column 1.
   import com.atlassian.jira.issue.MutableIssue
   ^

Script1.groovy: 4: unable to resolve class com.atlassian.jira.bc.project.component.ProjectComponent
 @ line 4, column 1.
   import com.atlassian.jira.bc.project.component.ProjectComponent
   ^

Script1.groovy: 3: unable to resolve class com.atlassian.jira.component.ComponentAccessor
 @ line 3, column 1.
   import com.atlassian.jira.component.ComponentAccessor
   ^

3 errors

Hi @Tim Willmott ,

You said that works below:

issueInput.fields.components = [[name: 'Operations1']]

Could You please check next:

def array = issueInput.fields.components.clone()


List clone=[]

array.each{
clone.add(it)
}
clone.add([name: 'Operations1'])


issueInput.fields.components = clone.toArray()

B.R.

Thanks for trying to help. Got the following error:

JIRA static error.png

It mean issueInput.fields.components is List. Therefore should work 

issueInput.fields.components.add([name: 'Operations1'])

Have you checked this option?

B.R.

Just using that one line (twice, once for each component) didn't cause any static errors but gave me:

2019-04-17 15:03:06.502 ERROR - Cannot invoke method add() on null object on line 1
2019-04-17 15:03:06.602 ERROR - Class: com.adaptavist.sr.cloud.workflow.UpdateIssue, Config: [className:com.adaptavist.sr.cloud.workflow.UpdateIssue, uuid:3f47bc8a-64d6-4100-840b-77be047f1861, description:Add Operations1 and Operations2 as Components, enabled:true, executionUser:ADD_ON, additionalCode:issueInput.fields.components.add([name: 'Operations2'])
issueInput.fields.components.add([name: 'Operations1']), userKey:e01807, accountId:5cb5998a9a857910896fd759]

on execution 

any other ideas?

Hi @Tim Willmott ,

Sorry for long time response. Was a lots job :-)
I reactivate my cloud jira and found resolution.

Both below work for me:


issueInput.update.components = [[add: [name:"A"]],[add: [name:"B"]]]

Or:

List comps = []

comps.add([add: [name:"A"]])

comps.add([add: [name:"B"]])

issueInput.update.components = comps

Other possible commands:

Supported operation(s) are: 'add,set,remove'

 

B.R.

Like Tim Willmott likes this

Sorry for the long delay - been busy! Your first suggestion worked a treat! Thank you so much for your help!

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted in Jira

Calling all Jira Cloud users! Give us feedback on our exploration of a new navigation.

Hi everyone! My name’s Matt and I’m a product manager at Atlassian. I work in the navigation & findability space for all our Jira Cloud products. We’ve been working on trying to improve the exp...

1,045 views 16 12
Join discussion

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you