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)

 

 

Error:
 

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 like:log.info("...") 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$format.call(Unknown Source)
	at Script649.run(Script649.groovy:16)

2 answers

This widget could not be displayed.
Vasiliy Zverev Community Champion Nov 09, 2016

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();

issue.setCustomFieldValue(
        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.

 

 

Vasiliy Zverev Community Champion Nov 14, 2016

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?

 

This widget could not be displayed.

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 Aug 22, 2018 in Marketplace Apps

How a Marketplace app tech team is achieving gender diversity

Hello! My name is Genevieve Blanch, and I'm the Marketing Manager at RefinedWiki, creators of apps to give teams the tools to customize Atlassian platforms. Currently, 44% of the tech team at Re...

525 views 3 19
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