Groovy script to update custom field in post function

Andrew Thorne March 29, 2018

Hi,

I am trying to write a Groovy script to update the field Epic Link with a default value if the Epic Link was not set.

After searching the web and reading some articles, I found this code, which I have modified to only do the update if the current value if null:

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

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()
def epicIssue = issueManager.getIssueObject("JIRAMTCE-10") // Epic issue key here
def epicLink = customFieldManager.getCustomFieldObjectByName("Epic Link")

if (issue.getCustomFieldValue(epicLink) == null) {
log.info("Setting the Epic Link to JIRAMTCE-10")
issue.setCustomFieldValue(epicLink,epicIssue)
} else {
log.info("Epic Link was not null")
}

I also added the issue.setCustomFieldValue line as the original line gave an error.

FYI: Here is the original line that I replaced with the issue.setCustomFieldValue:

epicLink.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(epicLink), epicIssue),new DefaultIssueChangeHolder())

The code is placed as a Post Function to the workflow transition.

Finally I would like to say that this is my first attempt at Groovy so I imagine that the mistake is a very basic error because of my inexperience.

Any advice you can give will help me learn.

2 answers

1 vote
Andrew Thorne March 30, 2018

I still can't get it to work. I have modified my code and now it looks like this:

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

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()
def epicIssue = issueManager.getIssueObject("JIRAMTCE-10") // Epic issue key here
def epicLink = customFieldManager.getCustomFieldObjectByName("Epic Link")

log.error("epicIssue: " + epicIssue)
log.error("epicLink: " + epicLink)

if (issue.getCustomFieldValue(epicLink) == null) {
// epicLink.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(epicLink), epicIssue),new DefaultIssueChangeHolder())
log.error("Setting the Epic Link to JIRAMTCE-10")
// issue.setCustomFieldValue(epicLink,epicIssue)
def newModVal = new ModifiedValue(issue.getCustomFieldValue(epicLink), epicIssue)
log.error("newModVal: " + newModVal)
def newDICH = new DefaultIssueChangeHolder()
log.error("newDICH: " + newDICH)
epicLink.updateValue(null, issue, newModVal, newDICH)
} else {
log.error("Epic Link was not null")
}

 

The error I get is now this:

Time (on server): Fri Mar 30 2018 14:11:58 GMT+0200 (Romance Daylight Time)

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

2018-03-30 14:11:58,068 ERROR [workflow.ScriptWorkflowFunction]: epicIssue: JIRAMTCE-10
2018-03-30 14:11:58,069 ERROR [workflow.ScriptWorkflowFunction]: epicLink: Epic Link
2018-03-30 14:11:58,069 ERROR [workflow.ScriptWorkflowFunction]: Setting the Epic Link to JIRAMTCE-10
2018-03-30 14:11:58,069 ERROR [workflow.ScriptWorkflowFunction]: newModVal: com.atlassian.jira.issue.ModifiedValue@b10b2ab
2018-03-30 14:11:58,069 ERROR [workflow.ScriptWorkflowFunction]: newDICH: com.atlassian.jira.issue.util.DefaultIssueChangeHolder@1f
2018-03-30 14:11:58,081 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2018-03-30 14:11:58,081 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: GODZILLA-14, actionId: 1, file: <inline script>
java.lang.NullPointerException
 at com.atlassian.jira.issue.link.DefaultIssueLinkManager.getIssueLink(DefaultIssueLinkManager.java:373)
 at com.atlassian.jira.issue.link.DefaultIssueLinkManager.createIssueLink(DefaultIssueLinkManager.java:83)
 at com.atlassian.plugin.util.ContextClassLoaderSettingInvocationHandler.invoke(ContextClassLoaderSettingInvocationHandler.java:26)
 at com.sun.proxy.$Proxy27.createIssueLink(Unknown Source)
 at com.atlassian.plugin.osgi.bridge.external.HostComponentFactoryBean$DynamicServiceInvocationHandler.invoke(HostComponentFactoryBean.java:136)
 at com.sun.proxy.$Proxy27.createIssueLink(Unknown Source)
 at com.atlassian.greenhopper.manager.issuelink.EpicLinkManagerImpl.createLink(EpicLinkManagerImpl.java:322)
 at com.atlassian.greenhopper.manager.issuelink.EpicLinkManagerImpl.updateLinksForIssues(EpicLinkManagerImpl.java:238)
 at com.atlassian.greenhopper.manager.issuelink.EpicLinkManagerImpl.associateIssuesWithEpic(EpicLinkManagerImpl.java:179)
 at com.atlassian.greenhopper.service.issuelink.EpicServiceImpl.addIssuesToEpic(EpicServiceImpl.java:103)
 at com.atlassian.greenhopper.customfield.epiclink.EpicLinkCFType.associateIssueWithEpic(EpicLinkCFType.java:686)
 at com.atlassian.greenhopper.customfield.epiclink.EpicLinkCFType.createValue(EpicLinkCFType.java:202)
 at com.atlassian.greenhopper.customfield.epiclink.EpicLinkCFType.createValue(EpicLinkCFType.java:44)
 at com.atlassian.jira.issue.fields.ImmutableCustomField.createValue(ImmutableCustomField.java:693)
 at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:410)
 at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:396)
 at com.atlassian.jira.issue.fields.OrderableField$updateValue.call(Unknown Source)
 at Script109.run(Script109.groovy:21)

 Any clues?

Andrew Thorne April 4, 2018

Looking at the error messages, does the fact is seems to think that the field is immutable important? I do have update access to the issue as I am creating it.

0 votes
Alexey Matveev
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 29, 2018

What was the error?

Andrew Thorne March 29, 2018

Hi, Alexey,

The error I got from the original line was:

Time (on server): Fri Mar 30 2018 07:16:47 GMT+0200 (Romance Daylight Time)

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

2018-03-30 07:16:47,894 ERROR [workflow.ScriptWorkflowFunction]: ************************************************************************************* 2018-03-30 07:16:47,903 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: GODZILLA-9, actionId: 1, file: <inline script> java.lang.NullPointerException at com.atlassian.jira.issue.link.DefaultIssueLinkManager.getIssueLink(DefaultIssueLinkManager.java:373) at com.atlassian.jira.issue.link.DefaultIssueLinkManager.createIssueLink(DefaultIssueLinkManager.java:83) at com.atlassian.plugin.util.ContextClassLoaderSettingInvocationHandler.invoke(ContextClassLoaderSettingInvocationHandler.java:26) at com.sun.proxy.$Proxy27.createIssueLink(Unknown Source) at com.atlassian.plugin.osgi.bridge.external.HostComponentFactoryBean$DynamicServiceInvocationHandler.invoke(HostComponentFactoryBean.java:136) at com.sun.proxy.$Proxy27.createIssueLink(Unknown Source) at com.atlassian.greenhopper.manager.issuelink.EpicLinkManagerImpl.createLink(EpicLinkManagerImpl.java:322) at com.atlassian.greenhopper.manager.issuelink.EpicLinkManagerImpl.updateLinksForIssues(EpicLinkManagerImpl.java:238) at com.atlassian.greenhopper.manager.issuelink.EpicLinkManagerImpl.associateIssuesWithEpic(EpicLinkManagerImpl.java:179) at com.atlassian.greenhopper.service.issuelink.EpicServiceImpl.addIssuesToEpic(EpicServiceImpl.java:103) at com.atlassian.greenhopper.customfield.epiclink.EpicLinkCFType.associateIssueWithEpic(EpicLinkCFType.java:686) at com.atlassian.greenhopper.customfield.epiclink.EpicLinkCFType.createValue(EpicLinkCFType.java:202) at com.atlassian.greenhopper.customfield.epiclink.EpicLinkCFType.createValue(EpicLinkCFType.java:44) at com.atlassian.jira.issue.fields.ImmutableCustomField.createValue(ImmutableCustomField.java:693) at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:410) at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:396) at com.atlassian.jira.issue.fields.OrderableField$updateValue.call(Unknown Source) at Script45.run(Script45.groovy:11)

with line 11 of my script being the epicLink.updateValue line.

With the issue.setCustomFieldValue line nothing happens.
Alexey Matveev
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 29, 2018

Kindly change your script to this one and have a look at the logs. Either epicIssue or epicLink are null and that 's why update does not work.

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

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()
def epicIssue = issueManager.getIssueObject("JIRAMTCE-10") // Epic issue key here
def epicLink = customFieldManager.getCustomFieldObjectByName("Epic Link")
log.error("epicIssue: " + epicIssue)
log.error("epicLink: " + epicLink)

if (issue.getCustomFieldValue(epicLink) == null) {
log.info("Setting the Epic Link to JIRAMTCE-10")
issue.setCustomFieldValue(epicLink,epicIssue)
} else {
log.info("Epic Link was not null")
}
Andrew Thorne March 29, 2018

Alexey,

I added the two log.error lines and ran the script again. On my log, I do not get the messages, just these:

2018-03-30 08:18:30,604 http-nio-127.0.0.1-8080-exec-28 ERROR THORA 498x315646x1 1tlrc0l 10.5.35.191,127.0.0.1 /secure/CreateIssueDetails.jspa [c.k.j.c.i.routines.user.UserFullNameRoutine] Empty username provided.
2018-03-30 08:18:30,520 http-nio-127.0.0.1-8080-exec-28 ERROR THORA 498x315646x1 1tlrc0l 10.5.35.191,127.0.0.1 /secure/CreateIssueDetails.jspa [c.k.j.c.i.routines.user.UserFullNameRoutine] Empty username provided.
2018-03-30 08:18:30,435 http-nio-127.0.0.1-8080-exec-28 ERROR THORA 498x315646x1 1tlrc0l 10.5.35.191,127.0.0.1 /secure/CreateIssueDetails.jspa [c.k.j.c.i.routines.user.UserFullNameRoutine] Empty username provided.
2018-03-30 08:18:30,374 http-nio-127.0.0.1-8080-exec-28 ERROR THORA 498x315646x1 1tlrc0l 10.5.35.191,127.0.0.1 /secure/CreateIssueDetails.jspa [c.k.j.c.i.routines.user.UserFullNameRoutine] Empty username provided.
2018-03-30 08:18:30,294 http-nio-127.0.0.1-8080-exec-28 ERROR THORA 498x315646x1 1tlrc0l 10.5.35.191,127.0.0.1 /secure/CreateIssueDetails.jspa [c.k.j.c.i.routines.user.UserFullNameRoutine] Empty username provided.
Andrew Thorne March 29, 2018

Just tried it again and now it says:

Time (on server): Fri Mar 30 2018 08:46:27 GMT+0200 (Romance Daylight Time)

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

2018-03-30 08:46:27,063 ERROR [workflow.ScriptWorkflowFunction]: epicIssue: JIRAMTCE-10
2018-03-30 08:46:27,063 ERROR [workflow.ScriptWorkflowFunction]: epicLink: Epic Link
2018-03-30 08:46:27,064 ERROR [workflow.ScriptWorkflowFunction]: Setting the Epic Link to JIRAMTCE-10

That looks correct to me. 

Andrew Thorne March 30, 2018

Alexey,

The original code I had:

epicLink.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(epicLink), epicIssue),new DefaultIssueChangeHolder())

Gives an error because issue.getCustomFieldValue(epicLink) is null. The issue.setCustomFieldValue is not saving the value, which might explain why I am getting nothing. The documentation I have found says that I should use updateValue, which I already know does not work.

I have another suggestion: Would epicLink.createValue(issue, epicIssue) be worth a go?

Andrew Thorne March 30, 2018

Nope, that did not work:

2018-03-30 09:11:22,562 ERROR [workflow.ScriptWorkflowFunction]: epicIssue: JIRAMTCE-10
2018-03-30 09:11:22,562 ERROR [workflow.ScriptWorkflowFunction]: epicLink: Epic Link
2018-03-30 09:11:22,562 ERROR [workflow.ScriptWorkflowFunction]: Setting the Epic Link to JIRAMTCE-10
2018-03-30 09:11:22,577 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2018-03-30 09:11:22,578 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: GODZILLA-13, actionId: 1, file: <inline script>
java.lang.NullPointerException
 at com.atlassian.jira.issue.link.DefaultIssueLinkManager.getIssueLink(DefaultIssueLinkManager.java:373)
 at com.atlassian.jira.issue.link.DefaultIssueLinkManager.createIssueLink(DefaultIssueLinkManager.java:83)
 at com.atlassian.plugin.util.ContextClassLoaderSettingInvocationHandler.invoke(ContextClassLoaderSettingInvocationHandler.java:26)
 at com.sun.proxy.$Proxy27.createIssueLink(Unknown Source)
 at com.atlassian.plugin.osgi.bridge.external.HostComponentFactoryBean$DynamicServiceInvocationHandler.invoke(HostComponentFactoryBean.java:136)
 at com.sun.proxy.$Proxy27.createIssueLink(Unknown Source)
 at com.atlassian.greenhopper.manager.issuelink.EpicLinkManagerImpl.createLink(EpicLinkManagerImpl.java:322)
 at com.atlassian.greenhopper.manager.issuelink.EpicLinkManagerImpl.updateLinksForIssues(EpicLinkManagerImpl.java:238)
 at com.atlassian.greenhopper.manager.issuelink.EpicLinkManagerImpl.associateIssuesWithEpic(EpicLinkManagerImpl.java:179)
 at com.atlassian.greenhopper.service.issuelink.EpicServiceImpl.addIssuesToEpic(EpicServiceImpl.java:103)
 at com.atlassian.greenhopper.customfield.epiclink.EpicLinkCFType.associateIssueWithEpic(EpicLinkCFType.java:686)
 at com.atlassian.greenhopper.customfield.epiclink.EpicLinkCFType.createValue(EpicLinkCFType.java:202)
 at com.atlassian.greenhopper.customfield.epiclink.EpicLinkCFType.createValue(EpicLinkCFType.java:44)
 at com.atlassian.jira.issue.fields.ImmutableCustomField.createValue(ImmutableCustomField.java:693)
 at com.atlassian.jira.issue.fields.OrderableField$createValue.call(Unknown Source)
 at Script62.run(Script62.groovy:17)
Alexey Matveev
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 30, 2018

I tried your script like this

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

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()
def epicIssue = issueManager.getIssueObject("JIRAMTCE-10") // Epic issue key here
def epicLink = customFieldManager.getCustomFieldObjectByName("Epic Link")
log.error("epicIssue: " + epicIssue)
log.error("epicLink: " + epicLink)

if (issue.getCustomFieldValue(epicLink) == null) {
log.info("Setting the Epic Link to JIRAMTCE-10")
epicLink.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(epicLink), epicIssue),new DefaultIssueChangeHolder())
} else {
log.info("Epic Link was not null")
}

 and It worked. I am not sure, why you have a error 

Suggest an answer

Log in or Sign up to answer