How to unset a Date Custom Field in JIRA using Java API

java
scriptrunner
jira-server

#1

Lets say I have a Date custom field, and I need to unset its value regardless of whether has a value or not, via the ScriptRunner Automation Plugin wherein I have a scheduler script that resets this Date Custom Field. This is the groovy function I wrote for this, note the last parameter which is the customfieldsId

def removeCustDateForIssue(ApplicationUser user, Issue issue, long dateCustomFieldId) {
  IssueService issueService = ComponentAccessor.getIssueService()
  IssueInputParameters issueInputParameters = issueService.newIssueInputParameters();
  // issueInputParameters.addCustomFieldValue(dateCustomFieldId, "3/Jan/2012")
  issueInputParameters.addCustomFieldValue(dateCustomFieldId, null).setSkipScreenCheck(true)
  UpdateValidationResult updateValidationResult = issueService.validateUpdate(user, issue.getId(), issueInputParameters);
      if (updateValidationResult.isValid()) {
          IssueResult updateResult = issueService.update(user, updateValidationResult)
          log.debug("Update result on date reset " + updateResult.toString())
          if (!updateResult.isValid()) {
              // Do something
              log.error("Cannot update the date for the issue :  " + issue.getKey() + " : " +  updateResult.getErrorCollection())
          }
      } else {
        log.error("Invalid operation for update : " + updateValidationResult.getErrorCollection())
      }
}`

When this function gets called, the date gets reset as expected but , I sometimes see this error in the logs:

2019-01-21 22:10:47,523 automation-rule-executor:thread-5 ERROR admin 906x45124x1 wrct1h 76.74.51.178 /rest/plugins/self-update/1.0/ [c.a.r.j.customfields.parent.ParentCustomFieldHelper] com.atlassian.rm.common.bridges.jira.issue.IssueLinkException: java.lang.IllegalArgumentException: Link cannot be null
com.atlassian.rm.common.env.EnvironmentServiceException: com.atlassian.rm.common.bridges.jira.issue.IssueLinkException: java.lang.IllegalArgumentException: Link cannot be null
	at com.atlassian.rm.jpo.env.issues.JiraIssueLinkService.deleteIssueLink(JiraIssueLinkService.java:145)
	at com.atlassian.rm.jpo.customfields.parent.issue.DefaultIssueParentService.clearJposParentChildLink(DefaultIssueParentService.java:67)
	at com.atlassian.rm.jpo.customfields.parent.issue.DefaultIssueParentService.createJposParentChildLink(DefaultIssueParentService.java:53)
	at com.atlassian.rm.jpo.customfields.parent.issue.DefaultIssueParentService.setParentId(DefaultIssueParentService.java:41)
	at com.atlassian.rm.jpo.customfields.parent.ParentCustomFieldHelper.setIssueParent(ParentCustomFieldHelper.java:97)
	at com.atlassian.rm.jpo.customfields.parent.ParentCustomFieldType.updateValue(ParentCustomFieldType.java:63)
	at com.atlassian.rm.jpo.customfields.parent.ParentCustomFieldType.updateValue(ParentCustomFieldType.java:17)
	at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:426)
	at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:396)
	at com.atlassian.jira.issue.managers.DefaultIssueManager.updateFieldValues(DefaultIssueManager.java:708)
	at com.atlassian.jira.issue.managers.DefaultIssueManager.updateIssue(DefaultIssueManager.java:673)
	at com.atlassian.jira.issue.managers.RequestCachingIssueManager.updateIssue(RequestCachingIssueManager.java:219)
	at com.atlassian.jira.bc.issue.DefaultIssueService.update(DefaultIssueService.java:329)
	at com.atlassian.jira.bc.issue.DefaultIssueService.update(DefaultIssueService.java:303)
	at com.atlassian.jira.bc.issue.IssueService$update$3.call(Unknown Source)
	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:136)
	at Script277.removeEstDeliveryDateForIssue(Script277.groovy:271)
	... 3 filtered
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:98)
	at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
	at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1225)
	at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1034)
	at groovy.lang.DelegatingMetaClass.invokeMethod(DelegatingMetaClass.java:151)
	at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl$3.invokeMethod(GroovyScriptEngineImpl.java:305)
	at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:68)
	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:51)
	at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:83)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:185)
	at Script277.resetEstimatedDeliveryDatesOfProjectsAndFeatures(Script277.groovy:138)
	... 3 filtered
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:98)
	at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
	at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1225)
	at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1034)
	at groovy.lang.DelegatingMetaClass.invokeMethod(DelegatingMetaClass.java:151)
	at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl$3.invokeMethod(GroovyScriptEngineImpl.java:305)
	at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:68)
	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:51)
	at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:83)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:185)
	at Script277.run(Script277.groovy:56)
	at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:321)
	at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:159)
	at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:233)
	at javax.script.ScriptEngine$eval.call(Unknown Source)
	at com.onresolve.scriptrunner.runner.ScriptRunnerImpl.runScriptAndGetContext(ScriptRunnerImpl.groovy:178)
	at com.onresolve.scriptrunner.runner.ScriptRunner$runScriptAndGetContext$4.callCurrent(Unknown Source)
	at com.onresolve.scriptrunner.runner.ScriptRunnerImpl.runStringAsScript(ScriptRunnerImpl.groovy:167)
	at com.onresolve.scriptrunner.runner.ScriptRunner$runStringAsScript$3.call(Unknown Source)
	at com.onresolve.scriptrunner.canned.jira.utils.CustomScriptDelegate.doScript(CustomScriptDelegate.groovy:70)
	at com.onresolve.scriptrunner.canned.jira.utils.CustomScriptDelegate$doScript$2.call(Unknown Source)
	at com.onresolve.scriptrunner.automation.ExecuteScriptIssueAction.execute(ExecuteScriptIssueAction.groovy:117)
	at com.codebarrel.jira.plugin.automation.module.legacy.PluggableComponentExecutor.execute(PluggableComponentExecutor.java:68)
	at com.codebarrel.jira.plugin.automation.module.legacy.PluggableComponentExecutor.execute(PluggableComponentExecutor.java:32)
	at com.codebarrel.automation.api.service.ComponentChainImpl.doExecute(ComponentChainImpl.java:100)
	at com.codebarrel.automation.api.service.SingleRuleExecutorServiceImpl.execute(SingleRuleExecutorServiceImpl.java:194)
	at com.codebarrel.jira.plugin.automation.queue.JiraAutomationQueueExecutor.lambda$null$3(JiraAutomationQueueExecutor.java:256)
	at com.codebarrel.jira.plugin.automation.service.execution.JiraThreadLocalExecutor.lambda$executeAs$0(JiraThreadLocalExecutor.java:36)
	at com.codebarrel.jira.plugin.automation.service.execution.JiraThreadLocalExecutor.executeAsWithResult(JiraThreadLocalExecutor.java:65)
	at com.codebarrel.jira.plugin.automation.service.execution.JiraThreadLocalExecutor.executeAsWithResult(JiraThreadLocalExecutor.java:52)
	at com.codebarrel.jira.plugin.automation.service.execution.JiraThreadLocalExecutor.executeAs(JiraThreadLocalExecutor.java:35)
	at com.codebarrel.jira.plugin.automation.queue.JiraAutomationQueueExecutor.lambda$processClaimedItem$4(JiraAutomationQueueExecutor.java:256)
	at com.atlassian.jira.util.concurrent.BoundedExecutor$1.run(BoundedExecutor.java:50)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:745)
Caused by: com.atlassian.rm.common.bridges.jira.issue.IssueLinkException: java.lang.IllegalArgumentException: Link cannot be null
	at com.atlassian.rm.common.bridges.jira.issue.link.IssueLinkServiceBridge70.deleteIssueLink(IssueLinkServiceBridge70.java:208)
	at com.atlassian.rm.jpo.env.issues.JiraIssueLinkService.deleteIssueLink(JiraIssueLinkService.java:137)
	... 73 more
Caused by: java.lang.IllegalArgumentException: Link cannot be null
	at com.atlassian.jira.issue.link.DefaultIssueLinkManager.removeIssueLinkInternal(DefaultIssueLinkManager.java:171)
	at com.atlassian.jira.bc.issue.link.DefaultIssueLinkService.delete(DefaultIssueLinkService.java:200)
	... 2 filtered
	at java.lang.reflect.Method.invoke(Method.java:498)
	at com.atlassian.plugin.util.ContextClassLoaderSettingInvocationHandler.invoke(ContextClassLoaderSettingInvocationHandler.java:26)
	at com.sun.proxy.$Proxy334.delete(Unknown Source)
	... 2 filtered
	at java.lang.reflect.Method.invoke(Method.java:498)
	at com.atlassian.plugin.osgi.bridge.external.HostComponentFactoryBean$DynamicServiceInvocationHandler.invoke(HostComponentFactoryBean.java:136)
	at com.sun.proxy.$Proxy334.delete(Unknown Source)
	at com.atlassian.rm.common.bridges.jira.issue.link.IssueLinkServiceBridge70.deleteIssueLink(IssueLinkServiceBridge70.java:198)
	... 74 more
2019-01-21 22:10:49,407 automation-rule-executor:thread-5 ERROR admin 906x45124x1 wrct1h 76.74.51.178 /rest/plugins/self-update/1.0/ [c.a.r.j.customfields.parent.ParentCustomFieldHelper] com.atlassian.rm.common.bridges.jira.issue.IssueLinkException: java.lang.IllegalArgumentException: Link cannot be null
com.atlassian.rm.common.env.EnvironmentServiceException: com.atlassian.rm.common.bridges.jira.issue.IssueLinkException: java.lang.IllegalArgumentException: Link cannot be null
	at com.atlassian.rm.jpo.env.issues.JiraIssueLinkService.deleteIssueLink(JiraIssueLinkService.java:145)
	at com.atlassian.rm.jpo.customfields.parent.issue.DefaultIssueParentService.clearJposParentChildLink(DefaultIssueParentService.java:67)
	at com.atlassian.rm.jpo.customfields.parent.issue.DefaultIssueParentService.createJposParentChildLink(DefaultIssueParentService.java:53)
	at com.atlassian.rm.jpo.customfields.parent.issue.DefaultIssueParentService.setParentId(DefaultIssueParentService.java:41)
	at com.atlassian.rm.jpo.customfields.parent.ParentCustomFieldHelper.setIssueParent(ParentCustomFieldHelper.java:97)
	at com.atlassian.rm.jpo.customfields.parent.ParentCustomFieldType.updateValue(ParentCustomFieldType.java:63)
	at com.atlassian.rm.jpo.customfields.parent.ParentCustomFieldType.updateValue(ParentCustomFieldType.java:17)
	at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:426)
	at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:396)
	at com.atlassian.jira.issue.managers.DefaultIssueManager.updateFieldValues(DefaultIssueManager.java:708)
	at com.atlassian.jira.issue.managers.DefaultIssueManager.updateIssue(DefaultIssueManager.java:673)
	at com.atlassian.jira.issue.managers.RequestCachingIssueManager.updateIssue(RequestCachingIssueManager.java:219)
	at com.atlassian.jira.bc.issue.DefaultIssueService.update(DefaultIssueService.java:329)
	at com.atlassian.jira.bc.issue.DefaultIssueService.update(DefaultIssueService.java:303)
	at com.atlassian.jira.bc.issue.IssueService$update$3.call(Unknown Source)
	at Script277.removeEstDeliveryDateForIssue(Script277.groovy:271)
	... 3 filtered
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:98)
	at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
	at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1225)
	at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1034)
	at groovy.lang.DelegatingMetaClass.invokeMethod(DelegatingMetaClass.java:151)
	at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl$3.invokeMethod(GroovyScriptEngineImpl.java:305)
	at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:68)
	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:51)
	at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:83)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:185)
	at Script277.resetEstimatedDeliveryDatesOfProjectsAndFeatures(Script277.groovy:138)
	... 3 filtered
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:98)
	at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
	at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1225)
	at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1034)
	at groovy.lang.DelegatingMetaClass.invokeMethod(DelegatingMetaClass.java:151)
	at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl$3.invokeMethod(GroovyScriptEngineImpl.java:305)
	at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:68)
	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:51)
	at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:83)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:185)
	at Script277.run(Script277.groovy:56)
	at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:321)
	at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:159)
	at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:233)
	at javax.script.ScriptEngine$eval.call(Unknown Source)
	at com.onresolve.scriptrunner.runner.ScriptRunnerImpl.runScriptAndGetContext(ScriptRunnerImpl.groovy:178)
	at com.onresolve.scriptrunner.runner.ScriptRunner$runScriptAndGetContext$4.callCurrent(Unknown Source)
	at com.onresolve.scriptrunner.runner.ScriptRunnerImpl.runStringAsScript(ScriptRunnerImpl.groovy:167)
	at com.onresolve.scriptrunner.runner.ScriptRunner$runStringAsScript$3.call(Unknown Source)
	at com.onresolve.scriptrunner.canned.jira.utils.CustomScriptDelegate.doScript(CustomScriptDelegate.groovy:70)
	at com.onresolve.scriptrunner.canned.jira.utils.CustomScriptDelegate$doScript$2.call(Unknown Source)
	at com.onresolve.scriptrunner.automation.ExecuteScriptIssueAction.execute(ExecuteScriptIssueAction.groovy:117)
	at com.codebarrel.jira.plugin.automation.module.legacy.PluggableComponentExecutor.execute(PluggableComponentExecutor.java:68)
	at com.codebarrel.jira.plugin.automation.module.legacy.PluggableComponentExecutor.execute(PluggableComponentExecutor.java:32)
	at com.codebarrel.automation.api.service.ComponentChainImpl.doExecute(ComponentChainImpl.java:100)
	at com.codebarrel.automation.api.service.SingleRuleExecutorServiceImpl.execute(SingleRuleExecutorServiceImpl.java:194)
	at com.codebarrel.jira.plugin.automation.queue.JiraAutomationQueueExecutor.lambda$null$3(JiraAutomationQueueExecutor.java:256)
	at com.codebarrel.jira.plugin.automation.service.execution.JiraThreadLocalExecutor.lambda$executeAs$0(JiraThreadLocalExecutor.java:36)
	at com.codebarrel.jira.plugin.automation.service.execution.JiraThreadLocalExecutor.executeAsWithResult(JiraThreadLocalExecutor.java:65)
	at com.codebarrel.jira.plugin.automation.service.execution.JiraThreadLocalExecutor.executeAsWithResult(JiraThreadLocalExecutor.java:52)
	at com.codebarrel.jira.plugin.automation.service.execution.JiraThreadLocalExecutor.executeAs(JiraThreadLocalExecutor.java:35)
	at com.codebarrel.jira.plugin.automation.queue.JiraAutomationQueueExecutor.lambda$processClaimedItem$4(JiraAutomationQueueExecutor.java:256)
	at com.atlassian.jira.util.concurrent.BoundedExecutor$1.run(BoundedExecutor.java:50)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:745)
Caused by: com.atlassian.rm.common.bridges.jira.issue.IssueLinkException: java.lang.IllegalArgumentException: Link cannot be null
	at com.atlassian.rm.common.bridges.jira.issue.link.IssueLinkServiceBridge70.deleteIssueLink(IssueLinkServiceBridge70.java:208)
	at com.atlassian.rm.jpo.env.issues.JiraIssueLinkService.deleteIssueLink(JiraIssueLinkService.java:137)
	... 70 more
Caused by: java.lang.IllegalArgumentException: Link cannot be null
	at com.atlassian.jira.issue.link.DefaultIssueLinkManager.removeIssueLinkInternal(DefaultIssueLinkManager.java:171)
	at com.atlassian.jira.bc.issue.link.DefaultIssueLinkService.delete(DefaultIssueLinkService.java:200)
	... 2 filtered
	at java.lang.reflect.Method.invoke(Method.java:498)
	at com.atlassian.plugin.util.ContextClassLoaderSettingInvocationHandler.invoke(ContextClassLoaderSettingInvocationHandler.java:26)
	at com.sun.proxy.$Proxy334.delete(Unknown Source)
	... 2 filtered
	at java.lang.reflect.Method.invoke(Method.java:498)
	at com.atlassian.plugin.osgi.bridge.external.HostComponentFactoryBean$DynamicServiceInvocationHandler.invoke(HostComponentFactoryBean.java:136)
	at com.sun.proxy.$Proxy334.delete(Unknown Source)
	at com.atlassian.rm.common.bridges.jira.issue.link.IssueLinkServiceBridge70.deleteIssueLink(IssueLinkServiceBridge70.java:198)
	... 71 more
2019-01-21 22:10:51,265 automation-rule-executor:thread-5 DEBUG admin 906x45124x1 wrct1h 76.74.51.178 /rest/plugins/self-update/1.0/

I am perplexed why is it telling me “Link Cannot be null”? Am I unsetting the date custom field in the right way ? Any feedback will be really helpful and appreciated.