Adding multiple components using scriptrunner cloud

E01807 April 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
Andrew
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.
April 16, 2019

Hi @E01807 ,

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

E01807 April 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
Andrew
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.
April 17, 2019

Hi @E01807 ,

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.

E01807 April 17, 2019

Thanks for trying to help. Got the following error:

JIRA static error.png

Andrew
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.
April 17, 2019

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

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

Have you checked this option?

B.R.

E01807 April 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 

E01807 April 18, 2019

any other ideas?

Andrew
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.
April 18, 2019

Hi @E01807 ,

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 # people like this
tim.willmott@newday.co.uk May 10, 2019

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

Hemant September 11, 2021

This works. Thanks !

0 votes
Andrew
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.
April 16, 2019

Hi @E01807 ,

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.

Suggest an answer

Log in or Sign up to answer