Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Copying Multiple Fields via Groovy

Zachary Singh November 1, 2023

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
PD Sheehan
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.
November 1, 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.

 

Zachary Singh November 2, 2023

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()
PD Sheehan
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.
November 2, 2023

Can you add and report back the output of 

log.info "multiFieldOptionList result is $multiFieldOptionList (${multiFieldOptionList.getClass()})
Zachary Singh November 2, 2023
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>.
PD Sheehan
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.
November 2, 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.

Zachary Singh November 2, 2023

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()})
PD Sheehan
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.
November 2, 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 */

}

}

Zachary Singh November 2, 2023

Unfortunately I am still getting the same error

 

 

PD Sheehan
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.
November 2, 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