Portfolio for Jira "Team" Field?

Judah May 14, 2020

I have a Portfolio for Jira Team field and I am trying to set it's value using a script runner script. 

 

def PTFValue = issue.getCustomFieldValue(portfolioTeamField)



As you can see, I can log the "value", "title" and "description" but when I try to pass the "value" of 5 into a setCustomFieldValue method I get a type cast error.

How do I access all of the options for that field and assign them to unique variables? They are not simply strings, as the logs show. Even though the "title" of that Team object is Profiles I cannot pass the string Profiles as a value for that field.  The "Description" value (calling PTFValue.getDescription()) appears to change when logged at different run times. 

 

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  



I need to assign the Team field's value but I cannot use a string or number. I want to access all of the Team field's options but I am unable to do so. 

Is there some trick to access Portfolio For Jira fields? Any documentation that may help? 

 

Thank you! 

4 answers

1 accepted

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

Brian LeTourneau February 23, 2021

Hello Judeboy, 
I'm trying to run your code to get a complete list of IDs for all my teams and I'm running into the following error after copying and pasting your code into the script console.

 

The script could not be compiled: <pre>org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script36.groovy: 5: unexpected token: @ @ line 5, column 59. s.api.team.GeneralTeamService @WithPlugi ^ 1 error </pre>

Logs:

2021-02-23 13:31:08,254 ERROR [common.UserScriptEndpoint]: ************************************************************************************* 2021-02-23 13:31:08,258 ERROR [common.UserScriptEndpoint]: Script console script failed: groovy.lang.MissingPropertyException: No such property: teamService for class: Script34 at Script34.run(Script34.groovy:2)

 

Any ideas?

Judah April 16, 2021

Do you have a syntax error? Possibly an @ somewhere? 

It looks like it is not loading the module you need to use "teamService" correctly

1 vote
Joe Pursel June 15, 2022

In @Judah s post of Jun 22, 2020, I verified both sets of code work.

Please visit How to set Portfolio Team field on an issue with ScriptRunner? to see an example to Edit/Set the Advanced Roadmaps Team field programatically.

0 votes
Corey Leighton April 13, 2023

OK, I cobbled this together with snippits from other posts, some sawdust and a generous use of the philosophy of 'even a blind squirrel can find a nut'.  This will find the team that matches the name you provide and then sets the Team field to that value.  It looks nice and clean insider a Scriptrunner Console, but its a blood bath if you load it up into an IDE (unless you can pull in the roadmaps API)

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.rm.teams.api.team.GeneralTeamService
import com.atlassian.rm.teams.api.team.Team
import com.atlassian.rm.teams.core.team.data.DefaultTeam
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
@WithPlugin("com.atlassian.teams")
@PluginModule GeneralTeamService teamService

ApplicationUser user = ComponentAccessor.jiraAuthenticationContext.loggedInUser

// useful for testing
//IssueManager issueManager = ComponentAccessor.issueManager
//String issueKey = 'ABC-477130'
//Issue issue = issueManager.getIssueObject(issueKey)

Issue anIssue = issue
MutableIssue mIssue = ComponentAccessor.issueManager.getIssueObject(anIssue.key)

CustomField teamCF = ComponentAccessor.customFieldManager.getCustomFieldObjects(anIssue).find { it.name == "Team" }
DefaultTeam existingTeamObject = anIssue.getCustomFieldValue(teamCF ) as DefaultTeam
DefaultTeam newTeamObject = findTeambyName("Agile Tools")

if (existingTeamObject != newTeamObject) {
mIssue.setCustomFieldValue(teamCF ,newTeamObject)
ComponentAccessor.issueManager.updateIssue(user, mIssue, EventDispatchOption.DO_NOT_DISPATCH, false)
}

DefaultTeam findTeambyName(String teamName) {
@WithPlugin("com.atlassian.teams")
@PluginModule GeneralTeamService teamService
Long rmTeamId = teamService.teamIdsWithoutPermissionCheck.find {teamService.getTeam(it,false).get()?.description?.title?.toLowerCase() == teamName.toLowerCase()}
Team rmTeamObject = teamService.getTeam(rmTeamId,false).get()
return rmTeamObject as DefaultTeam
}
0 votes
Paul Tierney April 20, 2022
// FYI, not using scriptrunner, but
// Using JMWE postfunction I can set Team to it's string id.  For example,
// if I want to set Team to "App Operations Support", I can do:

//  tm= issue.get ("Team")   // do this for an existing issue with the Team value you want.
//  log.info ("tm is $tm")
// log.info shows me that "App Operations Support" is 6

return "6"

Suggest an answer

Log in or Sign up to answer