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

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

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

Set Portfolio-for-Jira Team field value Edited

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! 

1 answer

1 accepted

0 votes
Answer accepted

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.

 

 

Thank you very much for this post.

Is there method documentation for  documentation GenralTeamService?

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;

Suggest an answer

Log in or Sign up to answer
TAGS

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