Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Script Runner script to update a custom field for a specific issue

Darren Shinkins
Contributor
December 21, 2015

Hi,

I'm pretty new to writing scripts, and I'm hoping this is a pretty simple one, but I can't get it to work from the script console.

I'm trying to write a simple script to update a custom field value for an issue specified in the script. The code I have is:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue

def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()

def issue = issueManager.getIssueObject(137007)
def textCf = customFieldManager.getCustomFieldObject(14602) 

issue.setCustomFieldValue(textCf, "MyText")

I'm starting pretty simple; just specifying the id for the issue, and the custom field. It doesn't give me any error, but it just doesn't work. Any ideas on what I'm missing?

 

Thanks

2 answers

1 accepted

2 votes
Answer accepted
Thanos Batagiannis [Adaptavist]
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 21, 2015

Hi Darren,

You can run the script below to your script console and will update the value for the text custom field with name "test CF" for the issue with key "Issue-key". 

import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.component.ComponentAccessor

def issueManager = ComponentAccessor.getIssueManager()
def currentIssue = issueManager.getIssueObject("Issue-Key")
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def tgtField = customFieldManager.getCustomFieldObjects(currentIssue).find {it.name == "test CF"}
def changeHolder = new DefaultIssueChangeHolder();
tgtField.updateValue(null, currentIssue, new ModifiedValue(currentIssue.getCustomFieldValue(tgtField), "new value"),changeHolder);

Please let me know if this works for you.

Regards

2 votes
Chander Inguva
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 22, 2015

Hey Darren,

                      Issue.setCustomFieldValue() is no longer working. We have to do it using changeholder and updatevalue as described above by Thanos.

 

Cheers

Chander Inguva

Suggest an answer

Log in or Sign up to answer
TAGS
atlassian, jira cloud certification, managing jira projects, jira project administration, jira cloud exam, atlassian certification, agile project management, jira workflows, jira permissions, jira training, jira cloud skills, atlassian learning

Become a Certified Jira Service Project Expert 🦸🏻‍♂️

Validate your expertise in managing Jira Service Projects for Cloud. Master configuration, optimize workflows, and manage users seamlessly. Earn global 🗺️ recognition and advance your career as a trusted Jira Service management expert.

Get Certified! ✍️
AUG Leaders

Atlassian Community Events