How to set Portfolio Team field on an issue with ScriptRunner?

Richard Lucas October 31, 2018

I'm attempting to set the value of the Team field to the same value as the Parent item on a workflow transition. The Team field is a Portfolio field, and seems to have a different api to Jira custom fields.

 

import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Level
import org.apache.log4j.Logger

Logger log = Logger.getLogger("sr.workflow.project.create")
log.setLevel(Level.DEBUG)

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def parentLinkField = customFieldManager.getCustomFieldObjectsByName("Parent Link")
def teamField = customFieldManager.getCustomFieldObjectsByName("Team")

log.debug("Created ${issue.key}")
def parentKey = issue.getCustomFieldValue(parentLinkField).toString()?:null
log.debug("Parent Link: ${parentKey}")

if(parentKey){
def parent = ComponentAccessor.issueManager.getIssueByCurrentKey(parentKey)
def team = parent.getCustomFieldValue(teamField)?:null
if (team){
log.debug("Parent Team: ${team}")
// Set the current issue 'Team' field
issue.setCustomFieldValue(parentLinkField, team)
}
}

And this is the result in the log 

groovy.lang.MissingMethodException: No signature of method:
com.atlassian.jira.issue.IssueImpl.setCustomFieldValue() is applicable for argument types:
(com.google.common.collect.SingletonImmutableList, com.atlassian.rm.teams.core.team.data.DefaultTeam) values: [[Parent Link], 461]
Possible solutions: setCustomFieldValue(com.atlassian.jira.issue.fields.CustomField, java.lang.Object), getCustomFieldValue(com.atlassian.jira.issue.fields.CustomField)
at Script237.run(Script237.groovy:22)

I've tried various combinations... none of the successful so thought its time to ask the hive mind :-)

Any suggestions would be welcome.

 

4 answers

2 accepted

1 vote
Answer accepted
Richard Lucas October 31, 2018

Hi @Tom Lister

Many thanks for the clues. I found that this worked:

import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Level
import org.apache.log4j.Logger

Logger log = Logger.getLogger("sr.workflow.project.create")
log.setLevel(Level.INFO)

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def parentLinkField = customFieldManager.getCustomFieldObjectByName("Parent Link")
def teamField = customFieldManager.getCustomFieldObjectByName("Team")

log.debug("Created ${issue.key}")
def parentKey = issue.getCustomFieldValue(parentLinkField).toString()?:null
log.debug("Parent Link: ${parentKey}")
log.debug("parentLinkField class: ${parentLinkField.class}")
log.debug("teamField class: ${teamField.class}")

def team = issue.getCustomFieldValue(teamField)?:null
if(team == null){
if(parentKey){
def parent = ComponentAccessor.issueManager.getIssueByCurrentKey(parentKey)
def parentTeam = parent.getCustomFieldValue(teamField)?:null
if (parentTeam){
log.debug("Parent Team: ${parentTeam}")
log.debug("Parent Team Name: ${parentTeam.getDescription().getTitle()}")
log.info("Setting the ${issue.key} 'Team' field to ${parentTeam.getDescription().getTitle()}")
issue.setCustomFieldValue(teamField, parentTeam)
}
} else{
log.info("Parent issue ${parentKey} does not have the Team set")
}
} else {
log.info("Current issue ${issue.key} already has a Team value ${team.getDescription().getTitle()} so will not update")
}

There was another subtle problem with my original code - I was using 

customFieldManager.getCustomFieldObjectByNames

which I changed to 

customFieldManager.getCustomFieldObjectByName

 And then corrected the typo 

 

issue.setCustomFieldValue(parentLinkField, team) 

to  

issue.setCustomFieldValue(teamField, parentTeam)  

Much to my surprise this does work on the 'Create' transition.

Thanks again for your help

0 votes
Answer accepted
Tom Lister
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 31, 2018

Hi @Richard Lucas

I spent a chunk of time yesterday looking at this in relation to post 

https://community.atlassian.com/t5/Jira-questions/Copy-value-portfolio-field-quot-Team-quot-in-custom-field/qaq-p/926689#M297939

The published API doesn't show any suitable methods and was unable to locate anything on class DefaultTeam. (I was considering using reflection to find declared methods)

one for the Portfolio team I think

Tom Lister
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 31, 2018

try this

team.getDescription().getTitle()

i found this method via reflection. It returns a team name on my server 

0 votes
Joe Pursel June 15, 2022

To set the Advanced Roadmaps Team field for an Issue, I created a Scriptrunner script and placed it in an Automation for Jira project rule when an issue is created in the Project.

Please review @Judah s scripts in How to set Portfolio Team field on an issue with ScriptRunner?  to get the Team IDs and the Team Names - dated June 22, 2020. You will need these in the script below.

From the output of @Judah s script, you will see the Teams listed in numerical order by ID followed by the Team Name. In the output, locate the Team Name and its ID.

The script below uses the Team ID obtained from the results of @Judah s script in the link above.

import com.onresolve.scriptrunner.runner.customisers.WithPlugin;

import com.onresolve.scriptrunner.runner.customisers.PluginModule;

import com.atlassian.rm.teams.api.team.GeneralTeamService;

import com.atlassian.jira.component.ComponentAccessor;

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

import com.atlassian.jira.issue.MutableIssue;

def im = ComponentAccessor.getIssueManager();
def cfm = ComponentAccessor.getCustomFieldManager();
def jac = ComponentAccessor.getJiraAuthenticationContext();

def mutableIssue = im.getIssueByCurrentKey(issue.getKey());

def um = ComponentAccessor.getUserManager();
def adminUser = um.getUserByName("USE YOUR ADMIN USER NAME");

@WithPlugin("com.atlassian.teams")

@PluginModule GeneralTeamService teamService

def team = teamService.getTeam(2).get() //Change '2' to the Team ID you want to use

def teamField = cfm.getCustomFieldObject("customfield_12345") //Use the custom field id of the Advanced Roadmaps Team field for your instance

mutableIssue.setCustomFieldValue(teamField, team);
im.updateIssue(adminUser, mutableIssue, EventDispatchOption.ISSUE_UPDATED, false);

Replace the '2' in the line below with the ID of the Team you want to insert into the field.

def team = teamService.getTeam(2).get()

Many thanks to @Judah and others who raised questions or provided information about what is needed to find the Advanced Roadmaps Team ID's and Names and how to use these to Edit the Team field programmatically.

0 votes
Judah May 14, 2020

@Tom Lister @Richard Lucas 

Follow up question for this post:

If I wanted to set the Team field's value "manually" and not copy from another field how would I access all of those options?

I do not want to copy the value from a parent but instead set the value based on another criteria. I obviously cannot call .getTitle on an empty field, since I am not copying that field how would I assign the value myself via a script?

It seems that calling .value() of the Team field when it is populated returns a number but I cannot pass an integer into that field when attempting to assign it myself...Any thoughts? 

Timeo Schmidt September 30, 2020

I have got the same issue as you. Did you solve it eventually ?

Kevin Phillips November 30, 2020

I've tested this exact code with the latest versions of Jira core, Portfolio (Advanced Roadmaps) and Script Runner and it does not work. You can query the list of teams, get references to the fields, and even read the values that are already selected in the field, but all my attempts at modifying the fields value programatically have failed thus far. Perhaps there's a bug in the Script Runner APIs that is preventing this from working, not sure.

Kevin Phillips November 30, 2020

For reference, here's a little helper function that I pieced together from a few other forum posts that extrapolates the Advanced Roadmaps team from the name of the team, which does work as expected. However, as I mentioned, all my attempts at assigning this value to the Team field to modify its value have thus far failed, so I'm not sure how much this will help:

def findTeamByName(def teamName) {    
@WithPlugin("com.atlassian.teams")
@PluginModule GeneralTeamService teamService
    def retval = null
    teamService.teamIdsWithoutPermissionCheck.each {
        def temp = teamService.getTeam(it).get()
        def title = temp.getDescription().getTitle()
        if (title.toLowerCase() == teamName.toLowerCase()) {
            retval = temp
            return
        }
    }
    return retval
}

Suggest an answer

Log in or Sign up to answer