Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

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
4,560,491
Community Members
 
Community Events
185
Community Groups

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.

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

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?

Regards,

Daniel

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

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

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. 

GeneralTeamService.createTeam(teamDescription) 

    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!

BR

Dima

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events