Script Runner set date field based on custom field

Trying to set the value for a field based on another field so the value is retained as the original value.


import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.CustomFieldManager
import java.sql.Timestamp
import org.joda.time.DateTime;
import org.apache.commons.lang.time.DateUtils;
import java.text.SimpleDateFormat;
import java.util.Date.*;

Issue issue = issue
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-YYYY");
def ocf = customFieldManager.getCustomFieldObjectByName("Expected Delivery Date")
def strDate = sdf.format(ocf).format("dd-MM-YYYY")
def cf = customFieldManager.getCustomFieldObjectByName("Original Delivery Date")


if (!cf)

issue.setCustomFieldValue(cf, strDate)




Time (on server): Wed Nov 09 2016 15:58:49 GMT-0600 (Central Standard Time)

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

2016-11-09 15:58:49,120 ERROR [customfield.GroovyCustomField]: *************************************************************************************
2016-11-09 15:58:49,121 ERROR [customfield.GroovyCustomField]: Script field failed on issue: EXCHH-64789, field: Original Delivery Date
java.lang.IllegalArgumentException: Cannot format given Object as a Date
	at java_text_Format$ Source)

2 answers

If both custom fields are date type then you could use this code:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();

        customFieldManager.getCustomFieldObjectByName("Expected Delivery Date")
        , customFieldManager.getCustomFieldObjectByName("Original Delivery Date"))

If not, say types of these custom fields.

Both custom fields are date fields and I am trying to have a scripted field set the original date only when the expected date is entered the 1st time.  If the expected date is changed we want the original to stay with what the 1st value was.  

You should not use a script field to make any changes for the issue - it should only read information and display a calculated value.

You have different options here - 

  1. Use a script field to display the first field value from the change history of the issue
  2. Use a listener to copy the current value to a standard date field, only if there is no previous values for it.



Lets start with:

  1. expected date is changed - how this date is changed: via transition, then postfunction could be used, or via issue edit - then you need isue listener
  2. Script field - is a custom filed which value is calculated by script. Is your fields are date type or scripted fields?


Looks like you are trying to set a Date CF to a String... set it to a Date. Vasiliy's code looks valid.

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Wednesday in Marketplace Apps

Marketplace Spotlight: Marketing apps for Confluence to keep your teams working on the same page


199 views 0 4
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you