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,467,826
Community Members
 
Community Events
177
Community Groups

Updating a custom field from a script listener

Users frequently want to move from using a Scripted Field to using a custom field that's automatically updated. This is useful for Jira Portfolio, which, as of this writing, doesn't support calculated fields. It's also useful if you need an automatic value that can be changed via the UI. The question is, how to do it in ScriptRunner?

1 answer

1 accepted

0 votes
Answer accepted

Answer: setup a Script Listener to listen for the Issue Updated event with code similar to the following (tweaked where noted)

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.Issue

def issue = event.issue as Issue
def desiredValue = issue.summary + issue.description //TODO: Change this to be the actual calculation you want
def customFieldName = "My Custom Field" //TODO: Change this to match the custom field you want
def customFieldManager = ComponentAccessor.customFieldManager
def field = customFieldManager.getCustomFieldObjects(issue).find{
it.name == customFieldName
}
def currentCustomFieldValue = issue.getCustomFieldValue(field)
if (currentCustomFieldValue == desiredValue) {
return //Quit -- we don't want to update the field if it's already correct.
}
def issueService = ComponentAccessor.getIssueService()
def issueInputParameters = issueService.newIssueInputParameters()
issueInputParameters.addCustomFieldValue(field.id, desiredValue)
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def validationResult = issueService.validateUpdate(loggedInUser,
issue.id,
issueInputParameters
)
if (validationResult.isValid()) {
def updateResult = issueService.update(loggedInUser,
validationResult,
EventDispatchOption.DO_NOT_DISPATCH, //This is important to avoid infinite loops
false)
if (!updateResult.isValid()) {
log.error "Failed to update issue $issue.key"
log.error updateResult.errorCollection.errorMessages
log.warn updateResult.warningCollection.warnings
}
} else {
log.error "Failed to update issue $issue.key"
log.error validationResult.errorCollection.errorMessages
log.warn validationResult.warningCollection.warnings
}

 

Kumar Rising Star Jan 18, 2019

Hi @Jonny Carter I have a requirement 

1) When the group field is changed then the "Assignee" field has to set to "Unassigned" automaticly

2) I have group field called "Assignment Group"  single group picker  so when ever  user assigned to to an issue in ""Assignee"(system field)  then the "Group " field has to update automatic  with the user belonged group

I'm trying a couple of scripts that are in community so there are not working

Can you please provide me the script please its will hleps me

 

Thanks,

Kumar

Hi @Kumar - Were you ever able to find a solution for this?  This is exactly what I'm trying to do as well. Thanks!

Hi   @Kumar ,did you get the script?

Suggest an answer

Log in or Sign up to answer