Updating a custom field using groovy (script runner)

Technical Operations September 11, 2013

I'm trying to update a custom field on a series of issues. I'm obviously missing something because the examples I'm culling from here on Answers give me the following error:

javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.issue.DocumentIssueImpl.setCustomFieldValue() is applicable for argument types: (com.atlassian.jira.issue.fields.CustomFieldImpl, java.lang.Integer) values: [CreatedInHelpdesk, 1] Possible solutions: getCustomFieldValue(com.atlassian.jira.issue.fields.CustomField)

Here's my code as of now:

import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.CustomFieldManager
 
jqlQuery = 'project != HELPDESK'
fromProject = 'HELPDESK'
result = ''

def customFieldManager = componentManager.getCustomFieldManager()
def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()
 
getFilterResult (jqlQuery,log).each{ Issue issue ->
    oldKeys = changeHistoryManager.getPreviousIssueKeys(issue.id)
    oldKeys.findAll{it==~/$fromProject.*/}.each{
        result += "$it -> ${issue.key}\n"
    }
    
    CustomField helpdeskField = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'CreatedInHelpdesk'}
    issue.setCustomFieldValue(helpdeskField, 1)
}
 
return result
 
List<Issue> getFilterResult(String jqlSearch, log) {
    def searchService = ComponentAccessor.getComponent(SearchService.class);
    def user = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()
    List<Issue> issues = null
 
    def parseResult =  searchService.parseQuery(user, jqlSearch);
    if (parseResult.isValid()) {
        def searchResult = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
        issues = searchResult.issues
    } else {
        log.error("Invalid JQL: " + jqlSearch);
    }
    return issues
}

2 answers

1 accepted

3 votes
Answer accepted
JamieA
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.
September 12, 2013

You need to get a MutableIssue, by using issueManager.getIssueObject(issue.id)

Then you can set the field value. But you should probably use IssueService to do a proper update.

Here is some code to update a field without creating a new change item and all that:

def tgtField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Name of custom field"}
def changeHolder = new DefaultIssueChangeHolder();
tgtField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(tgtField), "some new value"),changeHolder);

What type of field is it?

issue.setCustomFieldValue(helpdeskField, 1)

may not be right.

Technical Operations September 12, 2013

Thanks, exactly the pointer I needed.

Needed this, as well:

import com.atlassian.jira.issue.ModifiedValue

The code snippet that worked for me:

(Observant folks will note that since the setting snippet isn't inside the 'findAll', it's updating _every_ issue in the result. Oops. But at least it worked!)

getFilterResult (jqlQuery,log).each{ Issue issue ->
    oldKeys = changeHistoryManager.getPreviousIssueKeys(issue.id)
    oldKeys.findAll{it==~/$fromProject.*/}.each{
        result += "$it -> ${issue.key}\n"
    }
    CustomField helpdeskField = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'CreatedInHelpdesk'}
    helpdeskField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(helpdeskField), 1 as Double),changeHolder);
}
Bhargavi Nannapaneni May 25, 2015

Hi @Jamie Echlin [Adaptavist] , I have a similar requirement. In the code snippet given by you, is it possible to pass a list of values and have one of them select manually? My requirement is to make a field(component, versions etc) editable before the issue gets cloned. This means, I donot want the parent values to be copied on the clone issues and it should give me an option to select the values myself before issue gets clone. Please let me know if this can be done.

Daniel Alonso September 25, 2019

How can you adapt this to a user type custom field?

0 votes
Lisa Schaffer May 26, 2017

Hi @Jamie

I am using the clones and links script.  I want the condition to copy over 3 custom fields, but I can't figure out how to change the script to do that.

I tried to add 3 custom fields names in the {'cf1,cf2,cf3'} but it didn't work, and I can't repeat the code on line 3.

This is the code that I am using.

def cf = customFieldManager.getCustomFieldObjects(sourceIssue).find {it.name == 'Name of CF'}
issue.setCustomFieldValue(cf, sourceIssue.getCustomFieldValue(cf))

Any help would be greatly appreciated.

 

Suggest an answer

Log in or Sign up to answer