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

Groovy script to update custom field in post function

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

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?

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

What was the error?

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.

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

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.

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. 

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?

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)

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
Community showcase
Posted in Jira

Calling all Jira Cloud users! Give us feedback on our exploration of a new navigation.

Hi everyone! My name’s Matt and I’m a product manager at Atlassian. I work in the navigation & findability space for all our Jira Cloud products. We’ve been working on trying to improve the exp...

811 views 13 12
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