Set Portfolio-for-Jira Team field value

Judah May 13, 2020

I want to assign a portfolio Team to a card based on the Jira project. For example: 

In my listener, if the issue being created is in Project "KSB" then the Portfolio Team field should be set to <team of my choice>

My problem: I am currently unable to set the Portfolio Team field's value using a string or an integer. What can I pass into the updateValue method as the updated value for the portfolioTeamField?

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.index.IssueIndexingService
import org.apache.log4j.Logger
import org.apache.log4j.Level

def issue = event.issue as Issue

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def portfolioTeamField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName("Team")[0]
def changeHolder = new DefaultIssueChangeHolder()
def issueIndexManager = ComponentAccessor.getComponent(IssueIndexingService)
def PTFValue = issue.getCustomFieldValue(portfolioTeamField)


if (issue.projectObject.key == "KSB"){

//I NEED TO SET THE TEAM FIELD VALUE BELOW, BUT GET A TYPECAST ERROR, what replaces <portfolio team of my choice>

//portfolioTeamField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(portfolioTeamField), <portfolio team of my choice>),changeHolder)

}

issueIndexManager.reIndex(issue)

If I try to set that field using a string I get the typecast err below: 

2020-05-13 14:20:18,725 ERROR [runner.AbstractScriptListener]: *************************************************************************************
2020-05-13 14:20:18,726 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: null
java.lang.ClassCastException: java.lang.String cannot be cast to com.atlassian.rm.teams.api.team.Team
	at com.atlassian.rm.teams.customfields.team.TeamCustomFieldType.createValue(TeamCustomFieldType.java:22)
	at com.atlassian.jira.issue.fields.ImmutableCustomField.createValue(ImmutableCustomField.java:693)
	at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:410)
	at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:396)
	at com.atlassian.jira.issue.fields.OrderableField$updateValue.call(Unknown Source)
	at Script128.run(Script128.groovy:28)

If I manually set the field via the GUI and then log it's value this is what I get:

2020-05-13 14:31:58,687 DEBUG [runner.AbstractScriptRunner]: PorfolitoTeamField =Team
2020-05-13 14:31:58,687 DEBUG [runner.AbstractScriptRunner]: PTFValue =5
2020-05-13 14:31:58,687 DEBUG [runner.AbstractScriptRunner]: PTFValue Title = Profiles
2020-05-13 14:31:58,687 DEBUG [runner.AbstractScriptRunner]: PTFValue Description = com.atlassian.rm.teams.api.team.data.DefaultTeamDescription@6ce8214c  

But back to my original question I can't simply set the value to "Profiles" as a string, I am stuck please help! 

2 answers

1 accepted

0 votes
Answer accepted
Judah June 22, 2020

In order to set the Team field, we will need to provide a value whose type is com.atlassian.rm.teams.api.team.Team. We can get one of these by using the GeneralTeamService:

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

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

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

@WithPlugin("com.atlassian.teams")

@PluginModule GeneralTeamService teamService 

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

where 123 is the numerical ID of the team. If you need a complete list of IDs for all your teams, for reference purposes, then you can run the following in the Script Console:

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

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

import com.atlassian.rm.teams.api.team.GeneralTeamService @WithPlugin("com.atlassian.teams")

@PluginModule GeneralTeamService teamService 

teamService.teamIdsWithoutPermissionCheck.collectEntries {[(it) :teamService.getTeam(it).get()?.description?.title]}

which will show you the ID and name of every team.

 

 

Vasily Prokhorov December 23, 2020

Thank you very much for this post.

Is there method documentation for  documentation GenralTeamService?

Vasily Prokhorov December 23, 2020

Nevermind, I was able to use JMWE Scripted (Groovy) Post-function to look up the methods using the package name.

ANyway, thank you very much for this post, as it simplifies my code for finding the team ID based on its name from:

//Get the total number of teams in the system
def int teamCount = (int)teamAPI.count()
if((teamCount%MAX_TEAMS_PER_CALL) > 0){
teamCount = (teamCount/MAX_TEAMS_PER_CALL) + 1;
}else{
teamCount = (teamCount/MAX_TEAMS_PER_CALL);
}

//Itterate throught each 100 teams
for (int i = 1; i <= teamCount; i++){
Iterator <TeamDTO> teamsIterator = teamAPI.findAll(i, MAX_TEAMS_PER_CALL).iterator();
while(teamsIterator.hasNext()){
TeamDTO team = (TeamDTO) teamsIterator.next();
//Check if the team name matches the one in FNMTeam field
if (team.title.value().equals(fnmTeamFieldValue)){
teamID = team.id.value();
}
}
}

To this:

//Get the map of all team names and corresponding IDs
def teamMap = teamService.teamIdsWithoutPermissionCheck.collectEntries {[(teamService.getTeam(it).get().description.title) :(it)]};

//Get the team ID corresponding to the FNMTeam field value
def teamID = teamMap.get(fnmTeamFieldValue)?.value;
Like Judah likes this
Pratyush Paliwal April 20, 2022

Hey, Can you please share the syntax for  

teamService.createTeam(teamDescription) 
what should be the value for teamDescription in order to create a team field using teamService.createTeam method. 
Or is there a better approach to create team using ScriptRunner. Please suggest. 
Chris Melville November 17, 2022

@Vasily Prokhorov can you post a list of the methods you identified for GeneralTeamService?  In particular, I am trying to set the Team field based on jira user name, so I need to look up the user in the team membership and then extract the TeamID.

Vasily Prokhorov November 17, 2022

Hi @Chris Melville ,

I used the Jira Miscellaneous Workflow Extensions plugin's Scripted (Groovy) operation on issue (JMWE app) post-function to identify the available methods as it allows you to enter a custom class/interface and pull the methods for it. Here are the methods I see:

  1. createTeam(TeanDescription arg)
  2. deleteTeam(long arg)
  3. getDeepTeam(long arg)
  4. getDeepTeams(Set<Long> arg)
  5. getTeam(long arg, boolean arg)
  6. getTeamIdsThatExist(Collection<Long> arg)
  7. getTeams(int arg, int arg)
  8. getTeams(Set<Long> arg)
  9. updateTeam(UpdateTeamRequest arg)

To get the user (resources) information I would recommend using the getDeepTeam(long arg) method, which takes the team ID and returns a DeepTeam object with a resources property that is a list of DeepResource objects that contain a Person object.

When I test using the script below I can get an anonymized user ID (e.g. JIRAUSER80519):

 

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

def GeneralTeamService teamService = getComponent(GeneralTeamService);

def deepTeam = teamService.getDeepTeam(17563)

return deepTeam.get().resources.first().person.description.jiraUser.get()

 

This may not be the most useful code snippet. However, it shows the chaining you can do to get data from a Team / DeepTeam object.

Like # people like this
0 votes
Chris Melville December 6, 2022

With @Vasily Prokhorov's excellent help, I wrote this Listener that others may find useful. 

 

/*
Listener: Looks up current assignee in Jira portfolio Team and sets the Team value to first found
*/
package Listeners
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
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.event.type.EventDispatchOption
import org.apache.log4j.Logger
import org.apache.log4j.Level
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.util.ImportUtils
def reIndexIssue(issue) {
boolean wasIndexing = ImportUtils.isIndexIssues()
ImportUtils.setIndexIssues(true)
ComponentAccessor.getComponent(IssueIndexingService.class).reIndex(issue)
ImportUtils.setIndexIssues(wasIndexing)
}
@WithPlugin("com.atlassian.teams")
@PluginModule GeneralTeamService teamService
Logger log = Logger.getLogger("com.melville.chris")
log.setLevel(Level.INFO)
IssueEvent issueEvent = event as IssueEvent
MutableIssue issue = issueEvent.issue
if (issue.issueType.isSubTask() || issue.assignee == null)
return
ApplicationUser user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
List assignee = [ issue.assignee.username, issue.assignee.key ] // deep team can have either value
Object cf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName('Team')
assert cf: "Custom Field 'Team' not found"
List teamList = [ 7, 8, 11, 421 ] // adjust for teams that should be set
Long team = teamList.find { Long t ->
teamService.exists(t) &&
teamService.getDeepTeam(t)?.get().getResources()?.find { it.person.description.jiraUser.get() in assignee }
}
team ? log.debug("Found $assignee in Team: $team") : log.info("Assignee $assignee not found in any Team")
if (team && issue.getCustomFieldValue(cf).toString() != team as String) {
issue.setCustomFieldValue(cf, teamService.getTeam(team,false).get())
ComponentAccessor.issueManager.updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)
reIndexIssue(issue)
log.debug("Updated ${issue.key} with ${team}")
}

Suggest an answer

Log in or Sign up to answer