Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
Community Members
Community Events
Community Groups

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


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("")

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: cannot be cast to

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.

2 answers

1 accepted

3 votes
Answer accepted

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.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

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.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!

Hi Daniel, Its a great piece of code and it helped me to set the customfield value. I need some help with the following createTeam method. I want to create a new entry in the Team. 


    I dont understand what needs to be passed as teamsDescription to this method. I could not find any helpful doc which explains this method

Hello @Daniel Jones _TNG_ 

is it possible to set a value as a "Behavior" for the " Team" field, because I don't have a "problem" yet.

Thanks you!



Suggest an answer

Log in or Sign up to answer

Atlassian Community Events