Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to set the display format in a behavior script on dd-mm-yyyy

Marco Brundel
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 17, 2021

Hi,

I want to copy the value of a Custom field Date picker on a Jira Service Management portal via a Behavior from an issue selected in an issue picker field.

The copy action works, but it is not displayed in the desired format.

How do I make sure it is displayed in the format dd-mm-yyyy?

 

Regards, Marco

 

current script:

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager

def issueManager = ComponentAccessor.getIssueManager() as IssueManager
def issuePickerCustomFieldValue = getFieldById(getFieldChanged()).value
def relatedIssue = issueManager.getIssueByCurrentKey(issuePickerCustomFieldValue as String)
def EinddatumprojectCustomField = getFieldByName("Einddatum project")
def EinddatumprojectCustomFieldObject = customFieldManager.getCustomFieldObjectsByName("Einddatum project")[0]
def EinddatumprojectCustomFieldValue = (EinddatumprojectCustomFieldObject ? relatedIssue.getCustomFieldValue(EinddatumprojectCustomFieldObject) : null) as String
if (EinddatumprojectCustomFieldValue) {
EinddatumprojectCustomField.setFormValue(EinddatumprojectCustomFieldValue)
}

 

2 answers

1 accepted

Suggest an answer

Log in or Sign up to answer
1 vote
Answer accepted
PD Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 17, 2021

When you retrieve the value from your custom field, don't coerce the type to String:

def EinddatumprojectCustomFieldValue = (EinddatumprojectCustomFieldObject ? relatedIssue.getCustomFieldValue(EinddatumprojectCustomFieldObject) : null) as String

Because when you do that, you get the default format and you can't change it.

Instead, keep it as a Timestamp  (the default data type for date in jira) so you can use the format method (with optional user timezone)

def EinddatumprojectCustomFieldValue = (EinddatumprojectCustomFieldObject ? relatedIssue.getCustomFieldValue(EinddatumprojectCustomFieldObject) : null) as java.sql.Timestamp
if (EinddatumprojectCustomFieldValue) {
EinddatumprojectCustomField.setFormValue(EinddatumprojectCustomFieldValue.format('dd-MM-yyyy'))

//to format date based on user tz instead of server tz
//def userTimeZone = ComponentAccessor.getComponent(com.atlassian.jira.timezone.TimeZoneManager).loggedInUserTimeZone
//EinddatumprojectCustomField.setFormValue(EinddatumprojectCustomFieldValue.format('dd-MM-yyyy',userTimeZone ))
}
Marco Brundel
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 18, 2021

@PD Sheehan Thanks!  It works!

2 votes
Nir Haimov
Community Champion
August 17, 2021

Hi @Marco Brundel 

When you set the date to your field , try to do it like this:

myField.setFormValue(newVal.format("dd-mm-yyyy"))
TAGS
AUG Leaders

Atlassian Community Events