Forums

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

Post function not updating custom date field

Roy Chapman March 15, 2019

Running this script as a post function

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.ComponentManager
import org.apache.log4j.Logger

def log = Logger.getLogger("com.acme.workflows")

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();

def eDate = customFieldManager.getCustomFieldObjectByName("End Date")
def eDatev = issue.getCustomFieldValue(eDate)

if ( eDatev == null )

{ log.warn "End Date is set to " + eDatev def sDate = customFieldManager.getCustomFieldObjectByName("Implementation Date") def sDatev = issue.getCustomFieldValue(sDate) log.warn "Implementation Date is set to " + sDatev issue.setCustomFieldValue( eDate, sDatev) }

eDatev = issue.getCustomFieldValue(eDate)
log.warn "End Date is set to " + eDatev

The log file reads

2019-03-15 11:14:58,980 WARN [acme.workflows]: End Date is set to null
2019-03-15 11:14:58,980 WARN [acme.workflows]: Implementation Date is set to 2019-03-20 00:00:00.0
2019-03-15 11:14:58,980 WARN [acme.workflows]: End Date is set to 2019-03-20 00:00:00.0

This shows the End Date is changed, as expected. But the value isn't saved. I have found this reference

https://community.atlassian.com/t5/Jira-questions/Scripted-post-function-to-set-a-date-value-in-custom-field/qaq-p/368278

It seems to suggest that the value will be saved. It is the 2nd item in a post function

What am I missing?

2 answers

1 accepted

0 votes
Answer accepted
Tarun Sapra
Community Champion
March 15, 2019

Hello @Roy Chapman 

The value is not changing because you are not doing it right.

You are using

issue.setCustomFieldValue( eDate, sDatev) }

From the docs about this method

Sets a custom field value on this Issue Object, but does not write it to the database. This is highly misleading. 

https://docs.atlassian.com/software/jira/docs/api/7.6.1/com/atlassian/jira/issue/MutableIssue.html#setCustomFieldValue-com.atlassian.jira.issue.fields.CustomField-java.lang.Object-

Tarun Sapra
Community Champion
March 15, 2019
Roy Chapman March 15, 2019

@Tarun Sapra 

Thank you.  I have amended the script and added these lines

(I have also done a bit of tidying up, but want to retain the thread in case someone else have the same issue)

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


def changeHolder = new DefaultIssueChangeHolder()
def eDatec = customFieldManager.getCustomFieldObjects(issue).find {it.name == "End Date"}
eDatec.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(eDatec), eDatev),changeHolder)

Works a treat now

Like Tarun Sapra likes this
0 votes
Antoine Berry
Community Champion
March 15, 2019

Hi, 

Maybe try to add this code at the end : 

import com.atlassian.jira.issue.index.IssueIndexingService

def issueIndexingService = ComponentAccessor.getComponent(IssueIndexingService)
issue.store()
issueIndexingService.reIndex(issue)

Suggest an answer

Log in or Sign up to answer