Set custom field value [Script Runner]

Aitor Bermejo Cacho
Contributor
March 27, 2019

Hello All,

I want to set a value for a custom field.

I try it to do it with a text field but didn't work...

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf = customFieldManager.getCustomFieldObjectByName("testgroovy2")

issue.setCustomFieldValue(cf,"Its Works")

How can I fixed it?

 

if I want to do it with a select user field....what I need to add?

 

Thank you all! 

 

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

6 votes
Answer accepted
Mark Markov
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 27, 2019

Hello @Aitor Bermejo Cacho 

You also need to save changes with IssueManager, like this

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

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManafer = ComponentAccessor.getIssueManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def cf = customFieldManager.getCustomFieldObjectByName("testgroovy2")
def userCf = customFieldManager.getCustomFieldObjectByName("user picker")

issue.setCustomFieldValue(cf,"Its Works")
issue.setCustomFieldValue(userCf, user)
issueManafer.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false)

 And to set User Picker, you need to pass ApplicationUser object.

Aitor Bermejo Cacho
Contributor
March 27, 2019

Thank you @Mark Markov , its works!

but, works if the field its text field...if I use a user picker?

Like Stephane Eicher likes this
Mark Markov
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 27, 2019

If it user picker, you need ApplicationUser object. Like in my example it sets currentUser in "user picker" field.

If you need some other user, use UserManager

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

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManafer = ComponentAccessor.getIssueManager()
def userManager = ComponentAccessor.getUserManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def userToSet = userManager.getUserByKey("userkey")
def userCf = customFieldManager.getCustomFieldObjectByName("user picker")

issue.setCustomFieldValue(userCf, userToSet)
issueManafer.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
Aitor Bermejo Cacho
Contributor
March 27, 2019

Thank you!

YogeshRanjit December 11, 2019

I am getting this error. Could you please help me.Untitled.png

Juan José Marchal Gómez
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 23, 2019

Hello @YogeshRanjit 

I think this is a typical error of beginer...

If you are doing the code in Console, please define one issue and use it.

If you are doing the code in postfunction or listened only is necessary to declare.

//Test para Console
def issueKey = "IESD-14862"
def issueManager = ComponentAccessor.getIssueManager()
def issue = issueManager.getIssueObject(issueKey)
//def issue = issue as Issue
log.info("issue: "+ issue)
Like # people like this
haventName February 14, 2020

OR

import com.atlassian.jira.user.util.UserManager

@StandardModule IssueManager issueManager

Issue issue = issueManager.getIssueByCurrentKey("ISSUEKEY-00000")

some code...

Like BrianB likes this
@Akshithrekulapelli March 24, 2022

errorrr.PNG

@Akshithrekulapelli March 24, 2022

can some one help me on this I want to multiple 3 different field values and display them on 4th field

Like Victoria Pantalone likes this