Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Creating Script Field of using template "Date"

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

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)

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

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

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?

It's working now Prasad! Mucho Gracias!

@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

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

Like Mike Schultz likes this

@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

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

0 votes

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

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
Community showcase
Published in Jira

Do you use Jira Cloud with Microsoft Teams?

Hi, Jira users! Do you use Jira alongside Microsoft Teams? We want to hear how you’ve used the power of Jira Cloud and Microsoft Teams (via the Jira Cloud for Microsoft Teams app) to achieve a team...

378 views 1 6
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you