Portfolio Java API: How can I update the value for Portfolio's "Team" field? Edited

I have a mapping of projects and teams and I want to set a default team upon creation of a ticket depending on the project. My solution is to use behaviours and write a script that updates the custom field "Team" accordingly whenever an issue is created.

I tested my code for updating the custom field value on an existing issue:

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

def issueManager = ComponentAccessor.issueManager
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def user = ComponentAccessor.getJiraAuthenticationContext().loggedInUser

def teamCf = customFieldManager.getCustomFieldObjectsByName('Team').first()

def issueToSet = issueManager.getIssueObject("XYZ")
long teamIdToSet = 41l //in my setup this is an existing ID for a specific team

def pluginAccessor = ComponentAccessor.getPluginAccessor()
Class teamAPIClass = pluginAccessor.getClassLoader().findClass("com.atlassian.rm.teams.publicapi.interfaces.team.TeamAPI")

def teamAPI = ComponentAccessor.getOSGiComponentInstanceOfType(teamAPIClass)
def teamValueGenerated = teamAPI.findById(teamIdToSet).value()

issueToSet.setCustomFieldValue(teamCf, teamValueGenerated)
issueManager.updateIssue(user, issueToSet, EventDispatchOption.ISSUE_UPDATED, false)

The result is a java.lang.ClassCastException: com.atlassian.rm.teams.publicapi.interfaces.team.TeamDTO cannot be cast to com.atlassian.rm.teams.api.team.Team

Apparently I am not using the public Portfolio Java API as intended. According to Atlassian, I am mixing the use of internal and public apis. The TeamDTO and the internal Jira Object Team "have almost the same structure though, so it’s possible that you could do some reflection hackery to make the types sync."

Any ideas how this could be resolved?

I am using the following versions:

  • Jira (Server) - v7.4.2
  • Portfolio for Jira - 2.15.0
  • Adaptavist ScriptRunner - 5.4.12 for Jira

Note that this question relates to Scriptrunner behaviors: How can I set a default value for Portfolio's "Team" field in JIRA? which has been resolved because back then there was no public API available.

1 answer

1 accepted

0 votes
Accepted answer

I raised a service request at Adaptavist and they were very fast and helpful at the same time. @Jake Choules replied the following:

Hi Daniel,

I've looked into this and it seems that there is another plugin component which produces Team objects of the correct type for the custom field, namely GeneralTeamService:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.rm.teams.api.team.GeneralTeamService
import com.atlassian.jira.event.type.EventDispatchOption
import com.onresolve.scriptrunner.runner.customisers.*;

@PluginModule GeneralTeamService ts

def TEAM_ID = 123L

def im = ComponentAccessor.issueManager
def u = ComponentAccessor.jiraAuthenticationContext.loggedInUser

def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName('Team')
def i = im.getIssueObject("XYZ")

i.setCustomFieldValue(cf, ts.getTeam(TEAM_ID).get())
im.updateIssue(u, i, EventDispatchOption.ISSUE_UPDATED, false)

(Note that the getTeam call returns an Optional<Team> so you may need to guard against the case where it comes back empty.)


Hi Daniel, thanks for working this out, two questions:

1. Can you explain what does the following do:

def i = im.getIssueObject("XYZ")

  2. What type of parameters did you use to set this behavior up with?  Was this set up as an Initialiser?  When does the Team field get set?

Thanks in advance!

Hi @J C,

1. That's just the code to retrieve the Issue Object. This is necessary when using the Script Console for testing. "XYZ" is the project key of the issue, where I tested the code.

2. I didn't use behaviours, instead I wrote a custom listener on Issue Created and Issue Moved.

Does that help?



Yes that makes total sense, thank you for the explanation!   I tried it as a listener and I'm getting the following:

cript function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: <inline script>
java.lang.NullPointerException: Cannot invoke method setCustomFieldValue() on null object
 at Script29.run(Script29.groovy:17)

do you know what might be causing that? 

I am afraid I would need a little more context for finding the cause of your issue. I will share my code with you, maybe that'll help

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.rm.teams.api.team.GeneralTeamService
import com.atlassian.jira.event.type.EventDispatchOption
import com.onresolve.scriptrunner.runner.customisers.*

@PluginModule GeneralTeamService teamService

final mappingProjectKeyTeam = [
'PK1': 1L,
'PK2': 2L

def issueManager = ComponentAccessor.issueManager
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def teamCf = ComponentAccessor.customFieldManager.getCustomFieldObjectsByName('Team').first()

def currentIssue = event.issue
def currentProjectKey = currentIssue.projectObject.key

def teamIdToSet = mappingProjectKeyTeam[currentProjectKey]
if (teamIdToSet == null) {
log.debug("No default Team was set. The project key $currentProjectKey of the current issue is missing in the configuration. Please add an according key value pair to the listener script.")

def teamValueToSet = teamService.getTeam(teamIdToSet)

if (!teamValueToSet.present)
log.debug("No default Team was set. There is no team assigned to the id $teamIdToSet. Please check the configuration in the listener script for project $currentProjectKey")
currentIssue.setCustomFieldValue(teamCf, teamValueToSet.get())
issueManager.updateIssue(user, currentIssue, EventDispatchOption.ISSUE_UPDATED, false)

The default value for the team needs to be set in several projects, that's why there is a mapping. In this example there are only two projects, actually there are a lot.

Thank you so much Daniel, just tried that as a listener and it worked perfectly!    Greatly appreciated!

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Feb 07, 2019 in Marketplace Apps

A Timeless Love Story

It started as any story starts, on a normal, rainy day.   Admin meets App, and her name was Klok2, and like any first relationship we were both trying to make it work but neither one knew what...

452 views 8 26
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you