It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

Daniel Oct 06, 2018

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
Answer accepted
Daniel Oct 15, 2018

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

@WithPlugin("com.atlassian.teams")
@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.)

Regards,
Jake

J C Oct 15, 2018

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!

Daniel Oct 16, 2018 • edited

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?

Regards,

Daniel

J C Oct 16, 2018

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? 

Daniel Oct 17, 2018

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

@WithPlugin("com.atlassian.teams")
@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.")
return
}

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")
return
}
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.

J C Oct 17, 2018

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
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Marketplace Apps & Integrations

scaling agile at the enterprise level with Valiantys

Hey there Community! My name is Vannya Vallejo, the Channel Communication Specialist at Atlassian and I want to help Atlassian users like you learn about our Solution Partners and how they can help ...

427 views 1 12
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you