Script Runner set date field based on custom field

Kevin Dalton November 9, 2016

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

1 vote
Vasiliy Zverev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 9, 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.

Kevin Dalton November 10, 2016

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.  

JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 13, 2016

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
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 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?

 

0 votes
JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 9, 2016

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
TAGS
AUG Leaders

Atlassian Community Events