Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Copying Multiple Fields via Groovy

Hi All-

 

I am trying create a script for updating multiple issue fields from a source field to a target field in one go. We have the following already determined for our field mappings: 

  • Multi Select to Single Select 
  • Single Select to Single Select 
  • Text to Text 
  • Single User to Multi User Field 

I am aware of the the build in ScriptRunner Job that allows us to utilize a filter, but we have to do it field by field and we have over 20+ fields and thousands of issues. 

Any help would be great!

1 answer

0 votes
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Nov 01, 2023

Is this a one-time execution? Or are you looking to have this operation repeated at a high frequency? I'm asking because you mentioned "Scriptrunner Job" which are designed to execute a specific task based on a pre-set schedule.

Either way, it doesn't much matter, the best option i, is to use the methods made available by HAPI.

Just in the Scriptrunner Console, you can use JQL and HAPI to perform a pre-determined set of field update operations on all issues.

 

import com.atlassian.jira.event.type.EventDispatchOption
def
jql = 'your jql to identify issues to update'

Issues.search(jql).each{issue->
    issue.update{
setSendEmail(false) //adjust as needed
setEventDispatchOption(EventDispatchOption.DO_NOT_DISPATCH) //adjust as needed

        def multiFieldOptionList = issue.getCustomFieldValue('MultiCustomField1')
        setCustomFieldValue('SingleSelect', multiFieldOptionList[0].value)
        /* ... etc for all your other mappings */
    }
}

HAPI will take care of all the underlying issue updates and identifying the correct option in the new field based on the text of the option value(s) of the old field.

 

Thanks Peter, so it doesn't seem to like the .value without passing it as string. 

No signature of method: com.adaptavist.hapi.jira.fields.DumbFieldUpdater.set() is applicable for argument types: ([Ljava.lang.Object;) values: [[1.0]] Possible solutions: set([Ljava.lang.String;), get(), use([Ljava.lang.Object;), wait(), any(), grep()
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Nov 02, 2023

Can you add and report back the output of 

log.info "multiFieldOptionList result is $multiFieldOptionList (${multiFieldOptionList.getClass()})
The script could not be compiled: <pre>org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script92.groovy: 16: Unexpected input: '"multiFieldOptionList result is $' @ line 16, column 10. log.info "multiFieldOptionList result is $multiFieldOptionList (${multiFieldOptionList.getClass()}) ^ 1 error </pre>.
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Nov 02, 2023

Maybe share your full script... all I supplied were example syntax. You have to adjust for your use case and field names and field types etc.

Apologies, I just took the HAPI portion that you provided and adjusted for my custom fields and jql. The test fields I was using are text from a single line to multi line.

 

import com.atlassian.jira.event.type.EventDispatchOption

def jql = 'issuekey=DT-13173'

Issues.search(jql).each{issue->

issue.update{

setSendEmail(false) //adjust as needed

setEventDispatchOption(EventDispatchOption.DO_NOT_DISPATCH) //adjust as needed

def multiFieldOptionList = issue.getCustomFieldValue('CR #')

setCustomFieldValue('Technical Design Details', multiFieldOptionList[0].value)

/* ... etc for all your other mappings */

}

}

log.info "multiFieldOptionList result is $multiFieldOptionList (${multiFieldOptionList.getClass()})
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Nov 02, 2023

Try with the log like this:

 

import com.atlassian.jira.event.type.EventDispatchOption

def jql = 'issuekey=DT-13173'

Issues.search(jql).each{issue->

issue.update{
setSendEmail(false) //adjust as needed
setEventDispatchOption(EventDispatchOption.DO_NOT_DISPATCH) //adjust as needed

def multiFieldOptionList = issue.getCustomFieldValue('CR #')
log.info "multiFieldOptionList result is $multiFieldOptionList (${multiFieldOptionList.getClass()}"
setCustomFieldValue('Technical Design Details', multiFieldOptionList[0].value)

/* ... etc for all your other mappings */

}

}

Unfortunately I am still getting the same error

 

 

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Nov 02, 2023

Did you copy the full script in the last reply? I had fixed an issue with a missing double-quote.

Maybe you can include a screenshot that shows the full script and any errors.

Here is an expanded example that I was able to test in my environment (except I left out the setCustomValuePart):

import com.atlassian.jira.event.type.EventDispatchOption

def jql = '"Automatic Sub-Tasks" is not empty'

def nameOfMultiSelectField = 'Automatic Sub-Tasks'
def nameOfSingleSelectField = 'Technical Design Details'
Issues.search(jql).take(1).each{issue->

issue.update{
setSendEmail(false) //adjust as needed
setEventDispatchOption(EventDispatchOption.DO_NOT_DISPATCH) //adjust as needed

def multiFieldOptionList = issue.getCustomFieldValue(nameOfMultiSelectField)
log.info "$issue.key: value of $nameOfMultiSelectField is $multiFieldOptionList (${multiFieldOptionList.getClass()})"
if(multiFieldOptionList instanceof List){
log.info "$issue.key: multiFieldOptionList.firstOption = ${multiFieldOptionList[0].value}"
} else {
log.error "$nameOfMultiSelectField is not returning a list of Options. This field doesn't work for this example."
}
//this will attempt to lookup an option in nameOfSingleSelectField that has the same string value as the first selected option in nameOfMultiSelectField. If there are no matching options, this will fail
//setCustomFieldValue(nameOfSingleSelectField, multiFieldOptionList[0].value)
/* ... etc for all your other mappings */
}
}

This is what it looks like when i execute:

2023-11-02 16_38_36-Script Console.png

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events