Posting a script runner date result to another date customfield

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

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

I forgot about custom output. Oops.

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

how can i use setCustomFieldValue

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

indexer?????? what is that

custom field -> edit -> searcher

currently it is date time range picker

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

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

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

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.

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"))

does not work

you don't need to include (Timestamp)

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]

without (Timestamp) also has the same error..

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

Hi Kristian, Errorr!!!!!!!

how can i use setCustomFieldValue

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

i want to write the output to a date customfield

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.

"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

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

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?

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 < 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

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.

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

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

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

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"))

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]

 

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

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]

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

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]

Hi Guys,

Any solution???????

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Tuesday in Statuspage

How do your teams prepare for really high (planned) traffic days like Cyber Monday?

Hi there! Shannon from Statuspage here.  👋  With Cyber Monday quickly approaching, we're looking to hear from Atlassian customers – specifically from teams who touch incident response li...

46 views 0 4
Join discussion

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