Forums

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

Update Custom Field with the Values from Two Other Fields (Automation for Jira)

David
Contributor
March 22, 2019

Hi,
I have Scriptrunner and Automation for Jira plugins.

I'm trying to have a scriptrunner action in Automation for Jira that updates a custom field with values from two other fields.

Namely, I'm trying to concatenate the Issue Key and the Issue Summary in the custom field.

Are you able to provide help?

2 answers

1 accepted

0 votes
Answer accepted
Mohamed Adel
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 24, 2019

@David 

try the following one 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.IssueInputParameters
import org.apache.log4j.Logger


def log = Logger.getLogger("com.onresolve.jira.groovy")

def userManager = ComponentAccessor.getUserManager()
def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("YourCustomFieldID")
def issueManager = ComponentAccessor.getIssueManager()
def user = userManager.getUserByName("YourAdmin")
def customFieldValue = issue.key +" "+ issue.summary


issue.setCustomFieldValue(customField,customFieldValue)
issueManager.updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)

Mohamed Adel
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 26, 2019

@David 

Update the following line

ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customFieldId=10711")

With 

ComponentAccessor.getCustomFieldManager().getCustomFieldObject(10711)

this code is working with the latest script runner version, please if you still have the problem mention your instance version 

David
Contributor
March 27, 2019

I have Version 5.5.0.

I changed the ("customFieldId=10711") to just (10711) but I'm still getting the same "cannot find matching method" error on the last two lines of the script.

I also tried swapping the last two lines of code to Antoine's suggestion but that gives me the following error:

unable to resolve class ModifiedValue
@ line 17, column 38.

unable to resolve class DefaultlssueChangeHolder
@ line 17, column 96.

 

 

Code is currently as follows:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.IssueInputParameters
import org.apache.log4j.Logger


def log = Logger.getLogger("com.onresolve.jira.groovy")

def userManager = ComponentAccessor.getUserManager()
def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(10711)
def issueManager = ComponentAccessor.getIssueManager()
def user = userManager.getUserByName("EPP Team")
def customFieldValue = issue.key +" "+ issue.summary


def customFieldOldValue = issue.getCustomFieldValue(customField)
customField.updateValue(null, issue, new ModifiedValue(customFieldOldValue, customFieldValue), new DefaultIssueChangeHolder())

Antoine Berry
Community Champion
March 27, 2019

Add these two imports : 

import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
David
Contributor
March 27, 2019

That did the trick, it's working now. Thank you!

0 votes
David
Contributor
March 25, 2019

I tried this but running into an error. Here is what I get:

[Static type checking] - Cannot find matching method

com.atlassian.jira.issue.Issue#setCustomFieldValue(com.atlassian.jira.iss
java.lang.String). Please check if the declared type is correct and if
the method exists.

@ line 16, column 1.

 

The code I'm running is:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.IssueInputParameters
import org.apache.log4j.Logger


def log = Logger.getLogger("com.onresolve.jira.groovy")

def userManager = ComponentAccessor.getUserManager()
def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customFieldId=10711")
def issueManager = ComponentAccessor.getIssueManager()
def user = userManager.getUserByName("EPP Team")
def customFieldValue = issue.key +" "+ issue.summary


issue.setCustomFieldValue(customField,customFieldValue)
issueManager.updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)

Antoine Berry
Community Champion
March 26, 2019

Hi, 

I would advise to swap the two last lines for these : 

def customFieldOldValue = issue.getCustomFieldValue(customField)
customField.updateValue(null, issue, new ModifiedValue(customFieldOldValue, customFieldValue), new DefaultIssueChangeHolder())

Antoine

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events