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

Tim Willmott Apr 16, 2019

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

0 votes
Андрей Жидков Apr 16, 2019 • edited Apr 17, 2019

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.

0 votes
Андрей Жидков Apr 16, 2019 • edited

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

Tim Willmott Apr 16, 2019

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
Андрей Жидков Apr 17, 2019 • edited

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.

Tim Willmott Apr 17, 2019

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.

Tim Willmott Apr 17, 2019

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 

Tim Willmott Apr 18, 2019

any other ideas?

Андрей Жидков Apr 18, 2019 • edited

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
Tim Willmott I'm New Here May 10, 2019

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
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Jira

Try Jira Cloud for Outlook: Organize your work without leaving your inbox

Hi Atlassian community, My name is Max and I work on the product integration team at Atlassian. I am pleased to announce the early access program for the Jira Cloud add-in for Outlook. This add-in...

1,319 views 3 14
Read article

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