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

How to set Portfolio Team field on an issue with ScriptRunner?

I'm attempting to set the value of the Team field to the same value as the Parent item on a workflow transition. The Team field is a Portfolio field, and seems to have a different api to Jira custom fields.

 

import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Level
import org.apache.log4j.Logger

Logger log = Logger.getLogger("sr.workflow.project.create")
log.setLevel(Level.DEBUG)

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def parentLinkField = customFieldManager.getCustomFieldObjectsByName("Parent Link")
def teamField = customFieldManager.getCustomFieldObjectsByName("Team")

log.debug("Created ${issue.key}")
def parentKey = issue.getCustomFieldValue(parentLinkField).toString()?:null
log.debug("Parent Link: ${parentKey}")

if(parentKey){
def parent = ComponentAccessor.issueManager.getIssueByCurrentKey(parentKey)
def team = parent.getCustomFieldValue(teamField)?:null
if (team){
log.debug("Parent Team: ${team}")
// Set the current issue 'Team' field
issue.setCustomFieldValue(parentLinkField, team)
}
}

And this is the result in the log 

groovy.lang.MissingMethodException: No signature of method:
com.atlassian.jira.issue.IssueImpl.setCustomFieldValue() is applicable for argument types:
(com.google.common.collect.SingletonImmutableList, com.atlassian.rm.teams.core.team.data.DefaultTeam) values: [[Parent Link], 461]
Possible solutions: setCustomFieldValue(com.atlassian.jira.issue.fields.CustomField, java.lang.Object), getCustomFieldValue(com.atlassian.jira.issue.fields.CustomField)
at Script237.run(Script237.groovy:22)

I've tried various combinations... none of the successful so thought its time to ask the hive mind :-)

Any suggestions would be welcome.

 

3 answers

2 accepted

1 vote
Answer accepted

Hi @tom lister

Many thanks for the clues. I found that this worked:

import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Level
import org.apache.log4j.Logger

Logger log = Logger.getLogger("sr.workflow.project.create")
log.setLevel(Level.INFO)

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def parentLinkField = customFieldManager.getCustomFieldObjectByName("Parent Link")
def teamField = customFieldManager.getCustomFieldObjectByName("Team")

log.debug("Created ${issue.key}")
def parentKey = issue.getCustomFieldValue(parentLinkField).toString()?:null
log.debug("Parent Link: ${parentKey}")
log.debug("parentLinkField class: ${parentLinkField.class}")
log.debug("teamField class: ${teamField.class}")

def team = issue.getCustomFieldValue(teamField)?:null
if(team == null){
if(parentKey){
def parent = ComponentAccessor.issueManager.getIssueByCurrentKey(parentKey)
def parentTeam = parent.getCustomFieldValue(teamField)?:null
if (parentTeam){
log.debug("Parent Team: ${parentTeam}")
log.debug("Parent Team Name: ${parentTeam.getDescription().getTitle()}")
log.info("Setting the ${issue.key} 'Team' field to ${parentTeam.getDescription().getTitle()}")
issue.setCustomFieldValue(teamField, parentTeam)
}
} else{
log.info("Parent issue ${parentKey} does not have the Team set")
}
} else {
log.info("Current issue ${issue.key} already has a Team value ${team.getDescription().getTitle()} so will not update")
}

There was another subtle problem with my original code - I was using 

customFieldManager.getCustomFieldObjectByNames

which I changed to 

customFieldManager.getCustomFieldObjectByName

 And then corrected the typo 

 

issue.setCustomFieldValue(parentLinkField, team) 

to  

issue.setCustomFieldValue(teamField, parentTeam)  

Much to my surprise this does work on the 'Create' transition.

Thanks again for your help

0 votes
Answer accepted
tom lister Community Leader Oct 31, 2018

Hi @Richard Lucas

I spent a chunk of time yesterday looking at this in relation to post 

https://community.atlassian.com/t5/Jira-questions/Copy-value-portfolio-field-quot-Team-quot-in-custom-field/qaq-p/926689#M297939

The published API doesn't show any suitable methods and was unable to locate anything on class DefaultTeam. (I was considering using reflection to find declared methods)

one for the Portfolio team I think

tom lister Community Leader Oct 31, 2018

try this

team.getDescription().getTitle()

i found this method via reflection. It returns a team name on my server 

@tom lister @Richard Lucas 

Follow up question for this post:

If I wanted to set the Team field's value "manually" and not copy from another field how would I access all of those options?

I do not want to copy the value from a parent but instead set the value based on another criteria. I obviously cannot call .getTitle on an empty field, since I am not copying that field how would I assign the value myself via a script?

It seems that calling .value() of the Team field when it is populated returns a number but I cannot pass an integer into that field when attempting to assign it myself...Any thoughts? 

I have got the same issue as you. Did you solve it eventually ?

I've tested this exact code with the latest versions of Jira core, Portfolio (Advanced Roadmaps) and Script Runner and it does not work. You can query the list of teams, get references to the fields, and even read the values that are already selected in the field, but all my attempts at modifying the fields value programatically have failed thus far. Perhaps there's a bug in the Script Runner APIs that is preventing this from working, not sure.

For reference, here's a little helper function that I pieced together from a few other forum posts that extrapolates the Advanced Roadmaps team from the name of the team, which does work as expected. However, as I mentioned, all my attempts at assigning this value to the Team field to modify its value have thus far failed, so I'm not sure how much this will help:

def findTeamByName(def teamName) {    
@WithPlugin("com.atlassian.teams")
@PluginModule GeneralTeamService teamService
    def retval = null
    teamService.teamIdsWithoutPermissionCheck.each {
        def temp = teamService.getTeam(it).get()
        def title = temp.getDescription().getTitle()
        if (title.toLowerCase() == teamName.toLowerCase()) {
            retval = temp
            return
        }
    }
    return retval
}

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