Creating Script Field of using template "Date"

Raju Kadam March 11, 2020

We need to display first value that Date field was set. Steps that I've are:

  1. From Change History, get 1st Change for this field
  2. Later using the Formatter object, convert it into a Date object.
  3. Return the formatted date to display using *Date* Template

 

{code}

//Find out date when device was deployed 1st time!

import com.atlassian.jira.component.ComponentAccessor
import static com.atlassian.jira.issue.IssueFieldConstants.*
import java.text.SimpleDateFormat;

def last_deployed_date_first_change_item = ComponentAccessor.getChangeHistoryManager().getChangeItemsForField(issue, "Last Deployed Date").first()

def formatter = new SimpleDateFormat("yyyy-MM-dd")
def first_deployed_date = formatter.parse(last_deployed_date_first_change_item?.to)
formatter.format(first_deployed_date)
{code}

 

When Date template selected, it throws $dateFormatterWithoutTime.format($value) error. But with Text Field (multi-line) it just works fine.

Since we need Date object that can be used in JQL later, we would like to create a field of type Date.

Let's know what's wrong with the script.

3 answers

1 accepted

0 votes
Answer accepted
Prasad Rambanam[ACIS] March 21, 2020

Hello Raju,

 

try with the following code

import com.atlassian.jira.component.ComponentAccessor
import java.text.SimpleDateFormat

def last_deployed_date_first_change_item = ComponentAccessor.getChangeHistoryManager().getChangeItemsForField(issue, "Last Deployed Date").first()

def formatter = new SimpleDateFormat("yyyy-MM-dd")
def first_deployed_date = formatter.parse(last_deployed_date_first_change_item?.to)
//def formattedDate = formatter.format(first_deployed_date)

return new Date(first_deployed_date.time)
Raju Kadam March 21, 2020

Prasad,

Thank you very much for your help. To make sure I've success using this code. Here is my current setup for *Script Field*

Template: Custom
Custom Template: $datePickerFormatter.withStyle($dateTimeStyle.DATE).format($value)
Script:

import com.atlassian.jira.component.ComponentAccessor
import java.text.SimpleDateFormat


def last_deployed_date_first_change_item = ComponentAccessor.getChangeHistoryManager().getChangeItemsForField(issue, "Last Deployed Date").first()

def formatter = new SimpleDateFormat("yyyy-MM-dd")
def first_deployed_date = formatter.parse(last_deployed_date_first_change_item?.to)

return new Date(first_deployed_date)

In the *Preview* it shows me a date like 03/11/20 which is correct.


But when I view the issue, I still get the same error:

First Deployed Date:$datePickerFormatter.withStyle($dateTimeStyle.DATE).format($value)

Am I missing anything here? Thanks again,

Raju

Prasad Rambanam[ACIS] March 21, 2020

Raju, why you are using custom template ?  you can use "Date Time" template right? 

Raju Kadam March 22, 2020

Prasad -- I've changed it to *Date Time* template, but that doesn't help much either. To keep in mind, when I preview it in ScriptRunner it shows me correct date-time. Also to debug it, I've just passed the current Date time object; but it's still not working. Now error that I get in View Issue screen is *Invalid Date*

Here is the latest code

import com.atlassian.jira.component.ComponentAccessor
import java.text.SimpleDateFormat

def last_deployed_date_changes = ComponentAccessor.getChangeHistoryManager().getChangeItemsForField(issue, "Last Deployed Date")
def first_change_date = last_deployed_date_changes.first()

def formatter = new SimpleDateFormat("yyyy-MM-dd")
def first_deployed_date = null;

if (first_change_date){
first_deployed_date = formatter.parse(first_change_date?.to)
}

return new Date()

Not sure what's wrong going on here.  Just curious, is it working in your setup?

Raju Kadam March 23, 2020

It's working now Prasad! Mucho Gracias!

Mike Schultz August 7, 2020

@Raju Kadam What was the fix for this? I have the exact problem. I see your code from March 22nd which you said did not work, what difference is there between that code and the working code? thanks

Prasad Rambanam[ACIS] August 7, 2020

@Mike Schultz fist thing, you need change the search template for customField  also can yous hare your code ?

Like Mike Schultz likes this
Raju Kadam August 12, 2020

@Mike Schultz - Sorry for delay in reply. I've put a detailed blog post on how to create this date field on my blog. Let me know if that helps:

 

https://raju.guide/index.php/2020/04/23/how-to-create-date-type-scriptrunner-script-field-in-jira/

 

Raju

Like Mike Schultz likes this
Mike Schultz November 3, 2020

@Raju Kadam @Prasad Rambanam[ACIS] I was able to figure this out but thank you for the responses!

0 votes
Shir Goldberg January 24, 2022

For anyone else with this issue, I found the answer in this blog post. You need to set the Search Template as a Date Time Range Picker instead of Freetext. You can do this by clicking on the search template currently listed next to the scripted field. 

0 votes
Leo
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 12, 2020

Hi @Raju Kadam,

it's long back I worked on this kind of scenario but I believe you need to add Template Value to store Date formats in scripted field

Template: Custom

Template Value: $datePickerFormatter.withStyle($dateTimeStyle.DATE).format($value)

 

 Hope this helps

 

BR,

Leo

Raju Kadam March 13, 2020

Leo -- Thanks for the suggestion. I've made changes as you suggested but still no luck. Here is how things are implemented

Screen Shot 2020-03-13 at 7.46.50 AM.png

But in the View Issue screen, I see date following way

Screen Shot 2020-03-13 at 7.47.00 AM.pngIs there anything I'm missing. Thank you!

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
8.3.4
TAGS
AUG Leaders

Atlassian Community Events