Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,559,469
Community Members
 
Community Events
184
Community Groups

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!

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

Atlassian Community Events