Posting a script runner date result to another date customfield

A November 24, 2015

I have a Scripted field this will calculate the date, but i want to append the result into an another customfield.

Reason Scripted field is storing the date and time. i want to store only date

6 answers

2 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 24, 2015

I think what you want to do is store a date and time but display only the date. Even the JIRA date custom field stores a date time IIRC, but the time is set to midnight, which you can do yourself if you want with with Date#clearTime().

A custom template that renders the date only could be:

#if ($value)
    $dateFieldFormat.format($value)
#end
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 24, 2015

I forgot about custom output. Oops.

A November 24, 2015

Hi Jamie, Thanks, But if i use above template, JIRA date query is not working

A November 24, 2015

how can i use setCustomFieldValue

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 24, 2015

> Thanks, But if i use above template, JIRA date query is not working what indexer is in use?

A November 24, 2015

indexer?????? what is that

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 25, 2015

custom field -> edit -> searcher

A November 25, 2015

currently it is date time range picker

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 25, 2015

> Thanks, But if i use above template, JIRA date query is not working How????? is it not working?

A November 25, 2015

I am using application called Eazybi to create the charts and if i change the template then i cannot do any operations to plot the chart. Simply the field dis appears from here

A November 25, 2015

Is it possible to write the output of scripted field to another date custom-field, It would really serve my purpose

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 25, 2015

It's really not the right thing to do, but you can use a listener to update another field. Do *not* do it from the script field code. It would be better to have EasyBI understand the formatting of a date.

A November 25, 2015

the code is as follows import com.atlassian.core.util.DateUtils def dateField = (Timestamp) getCustomFieldValue("Estimated End Date") log.error (dateField.format("d MMM yy"))

A November 25, 2015

does not work

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 25, 2015

you don't need to include (Timestamp)

A November 25, 2015

groovy.lang.MissingMethodException: No signature of method: org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.getCustomFieldValue() is applicable for argument types: (java.lang.String) values: [Estimated End Date]

A November 25, 2015

without (Timestamp) also has the same error..

1 vote
Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 24, 2015

Hi Amar,

 

I have looked at you code and have been able to format the returned Date/Time string using Java Simple Date format so that the returned output now shows the Day of the week, Day , Month and Year.

My refactored code is below with the two lines that are changed being lines 39 & 41 where I specify the date format and apply it to the newDate variable.

 

import com.atlassian.core.util.DateUtils
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.changehistory.ChangeHistoryManager
import java.text.SimpleDateFormat;
 
def dateField = new Date()
long numberField = (long) getCustomFieldValue("Duration to close")
  
MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject(issue.id);
ChangeHistoryManager changeHistoryManager = ComponentAccessor.getChangeHistoryManager();
if (numberField == null || numberField == 0 )
{    
def X = changeHistoryManager.getChangeItemsForField (issue, "status")
for (item in X?.reverse())
{   log.error("X = ${item.getToString()} .. ${item.created.format("d/MMM/yy")} ${item.created.getDateString()}");
      log.error("X = ${item.created}");
     def Y = item.created
     log.error (Y.format("d MMM yy"))
     return Y
     break;
  
}}
int number = numberField.intValue()
if (dateField) {
    Date result = dateField;
    Date newDate = new Date(result.getTime())
    int i = 0 ;
    while (i < number-1) {
        result = result + 1
        newDate = new Date(result.getTime())
        
        if (newDate[Calendar.DAY_OF_WEEK] == Calendar.SATURDAY || newDate[Calendar.DAY_OF_WEEK] == Calendar.SUNDAY) {
           number ++;
        }
         i ++;
    }
    // Specify the format that we want the date retuned in
    SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy")
   // Format the date that we want to return
   def formatNewDate = sdf.format(newDate)
   return formatNewDate
}
return

 

I hope this helps.

 

Kristian

A November 24, 2015

Hi Kristian, Errorr!!!!!!!

A November 24, 2015

how can i use setCustomFieldValue

Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 24, 2015

What do you mean by setCustomFieldValue. I used the code as a scripted field called "Close Date" using a text field renderer.

A November 24, 2015

i want to write the output to a date customfield

Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 24, 2015

You would be best to use Jamies template above the for to make the Field a Date / Time picker field. Otherwise use a script listener which updates a date field based on the value of the Duration to close field.

A November 25, 2015

"Otherwise use a script listener which updates a date field based on the value of the Duration to close field." How can i do that

A November 25, 2015

If i use Jamies template i am not able to plot charts using this scripted field

0 votes
A November 25, 2015

Hi Guys,

Any solution???????

0 votes
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 24, 2015

I don't think there's a way to make Script Runner see it as just a date - even if you set the time element to 0:0:0 (which is what JIRA does internally when storing date data in date/time fields), that is still going to come out as "midnight on <date>" when you look at the field.

I suspect your options are:

  • Change the scripted field to text output (which messes up sort and search)
  • As you suggest, create a "date" only custom field and write a listener that catches issue updates, and sets the field to the date.  Script Runner does listeners as well as scripted fields, and you can actually take the code for your scripted field and pretty much paste it into a listener.
A November 24, 2015

Hi Nic, Can you share the code to update a date customfield with the result

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 25, 2015

Should be just: issue.setCustomFieldValue(myDateCf, new Timestamp(resultOfCalculation)) The resultOfCalculation should be a date and time containing the full date and time, but if you define the field as a plain date (no time), JIRA will only store the date

A November 25, 2015

any imports needed??? as currently i am getting this error mListener] Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: <inline script> org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script283.groovy: 3: unable to resolve class Timestamp @ line 3, column 18. def dateField = (Timestamp) getCustomFieldValue("Estimated End Date") ^ error

A November 25, 2015

the code is as follows import com.atlassian.core.util.DateUtils def dateField = (Timestamp) getCustomFieldValue("Estimated End Date") log.error (dateField.format("d MMM yy"))

A November 25, 2015

Last error while doing

import com.atlassian.core.util.DateUtils
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.customfields.CustomFieldType
import java.sql.Timestamp; 
com.atlassian.jira.issue.IssueImpl;
MutableIssue myIssue = issue;
def Y = getCustomFieldValue("Estimated End Date")
log.error (Y.toTimestamp())
def X = (Y.toTimestamp())
issue.setCustomFieldValue("Close Date", X)
log.error(X)

 

groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.issue.IssueImpl.setCustomFieldValue() is applicable for argument types: (java.lang.String, java.sql.Timestamp) values: [Close Date, 2015-12-07 13:13:07.911]

 

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 25, 2015

That call expects you to pass it a custom field object, not a string. Try replacing that with a getCustomFieldByName call

A November 26, 2015

same error groovy.lang.MissingMethodException: No signature of method: org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.getCustomFieldByName() is applicable for argument types: (java.lang.String) values: [Close Date]

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 26, 2015

Ok, you need a manager class in there - CustomFieldManager.getCustomFieldByName

A November 26, 2015

groovy.lang.MissingMethodException: No signature of method: static com.atlassian.jira.issue.CustomFieldManager.getCustomFieldByName() is applicable for argument types: (java.lang.String) values: [Estimated End Date]

0 votes
A November 24, 2015

Below code gives me result in date and time.

I want only date. I cant get the date as scripted field does not support only date result.

I gives date and time.

So i want to write the result into another date customfield

import com.atlassian.core.util.DateUtils
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.changehistory.ChangeHistoryManager

def dateField = new Date()
long numberField = (long) getCustomFieldValue("Duration to close")
 
MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject(issue.id);
ChangeHistoryManager changeHistoryManager = ComponentAccessor.getChangeHistoryManager();
if (numberField == null || numberField == 0 )
{     
def X = changeHistoryManager.getChangeItemsForField (issue, "status")
for (item in X?.reverse())
{   log.error("X = ${item.getToString()} .. ${item.created.format("d/MMM/yy")} ${item.created.getDateString()}"); 
      log.error("X = ${item.created}"); 
     def Y = item.created
     log.error (Y.format("d MMM yy"))
     return Y
     break;
 
}}
int number = numberField.intValue()
if (dateField) {
    Date result = dateField;
    Date newDate = new Date(result.getTime())
    int i = 0 ;
    while (i &lt; number-1) {
        result = result + 1
        newDate = new Date(result.getTime())
        if (newDate[Calendar.DAY_OF_WEEK] == Calendar.SATURDAY || newDate[Calendar.DAY_OF_WEEK] == Calendar.SUNDAY) {
           number ++;
        }
         i ++;
    }
    return newDate
}
return
0 votes
Mark McCormack _Adaptavist_
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 24, 2015

Amar,

I'm not sure what you are asking here. Could you please clarify?

If you could share the code you are using for the scripted field, perhaps we can help you store just the date and not the date and time?

Suggest an answer

Log in or Sign up to answer