The Atlassian Community Forums are currently in read-only mode. We will be relaunching on a new platform on September 22 (read more here). We apologize for the extended downtime. For concerns or questions, please email communitymanagers@atlassian.com. See you on the other side, on the new Atlassian Community Forums! :)

×

Forums

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

ScriptRunner Groovy Script - Set summary based on custom fields in WF post-function in Jira 8

Tom104Tom
Contributor
April 7, 2020

Hi,

I'm trying to set the summary in a WF post-function based on selections of the issue type and a single-user-picker custom field.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.ApplicationUser

MutableIssue issue = issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()

def vIssueType = issue.getIssueType().name
def vUserPickerCF = customFieldManager.getCustomFieldObjectByName("myUserPickerCF")
def vUser = issue.getCustomFieldValue(vUserPickerCF) as ApplicationUser

issue.setSummary("[" + vIssueType + "] " + vUser.displayName + " (" + vUser.username + ")");

 The script worked in Jira 7, but while testing Jira 8 the following warnings were raised:

1.pngThe same appeared for def customFieldManager = ComponentAccessor.getCustomFieldManager()
2.png

So I corrected them all and ended up with

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.ApplicationUser

MutableIssue mutableIssue = issue
def cfManager= ComponentAccessor.getCustomFieldManager()

def vIssueType = issue.getIssueType().name
def vUserPickerCF = cfManager.getCustomFieldObjectsByName("myUserPickerCF")
def vUser = issue.getCustomFieldValue(vUserPickerCF) as ApplicationUser

issue.setSummary("[" + vIssueType + "] " + vUser.displayName + " (" + vUser.username + ")");

And now this error appears:

3.png

I tried to replace "issue." with "mutableIssue." but that doesnt work either, same error.

How should this code look like when upgrading to Jira 8? Is there maybe a simpler way of achieving this?

Thanks & Cheers, Tom

1 answer

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

0 votes
Alexander Dickbauer
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
January 31, 2021

Hello, 

It seems like you have multiple CFs with this name or maybe the function returns a Collection by default now. That's why I usually use ids but if you use get(n) (n beeing the index of the correct cf) you should be able to fetch your cf. 

cfManager.getCustomFieldObjectsByName("myUserPickerCF")

 

And you are using issue and mutableIssue, you should stick with mutableIssue.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.ApplicationUser

MutableIssue mutableIssue = issue
def cfManager= ComponentAccessor.getCustomFieldManager()

def vIssueType = mutableIssue.getIssueType().name
def vUserPickerCF = cfManager.getCustomFieldObjectsByName("myUserPickerCF")
def vUser = mutableIssue.getCustomFieldValue(vUserPickerCF) as ApplicationUser

mutableIssue.setSummary("[" + vIssueType + "] " + vUser.displayName + " (" + vUser.username + ")");

Even if you can get the info from a issue, with a mutableIssue you can get and set so mixing them up is not necessary and makes the code less readable in my opinion.

Hope that helped,

Cheers