We need to display first value that Date field was set. Steps that I've are:
{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.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Raju, why you are using custom template ? you can use "Date Time" template right?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Mike Schultz fist thing, you need change the search template for customField also can yous hare your code ?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Leo -- Thanks for the suggestion. I've made changes as you suggested but still no luck. Here is how things are implemented
But in the View Issue screen, I see date following way
Is there anything I'm missing. Thank you!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.