It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

3 votes
Answer accepted

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 Sign up to answer
Community showcase
Posted in Jira

We want to hear from you! How do you use Slack and Atlassian together?

Hi Everyone! My name is Mina and I am on Atlassian’s Ecosystems Marketing team. Our team is focused on our technology partnerships and marketplace apps. One of Atlassian’s partners is Slack, who ...

44 views 0 4
Join discussion

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you