Updating a custom field using groovy (script runner)

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

2 votes

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.

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);
}

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.

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 Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

3,327 views 14 20
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot