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,556,302
Community Members
 
Community Events
184
Community Groups

How to set a date field to current date using behaviors?

Hello,

Since the "default current date" option in Jira defaults to the issue created date, I'm trying to use behaviors to set the field to current date.

I'm using the below date to achieve this. The script is setting the date as expected but editing is not working i.e the field doesn't let the user save a new date, even if a new date is selected, the current date is set back again.

import com.onresolve.jira.groovy.user.FormField
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager;
import java.lang.Object
import java.util.Date
import java.sql.Timestamp


IssueManager issueManager = ComponentAccessor.getOSGiComponentInstanceOfType(IssueManager.class);
FormField formField = getFieldById("customfield_10111");

def date = new Date()
String newDate = date.format("dd/MMM/yy")

formField.setFormValue(newDate)

Please advice.

Thanks.

1 answer

You can try this something like

 

import com.atlassian.jira.component.ComponentAccessor
import java.text.SimpleDateFormat
import java.util.Date
import java.util.TimeZone


def cfHot = getFieldByName("HOT")
def cfHOTReason = getFieldByName("HOT Reason")
def cfHOTDateOn = getFieldByName("Became Hot On")

cfHOTDateOn.setReadOnly(true)

if (cfHot.value == null || cfHot.value == 'No' ){

cfHOTReason.setRequired(false)
cfHOTReason.clearError()
cfHOTDateOn.setFormValue(null)
cfHOTReason.setReadOnly(true)
cfHOTReason.setFormValue(null)


}

if (cfHot.value == 'Yes' ){


String DATE_FORMAT = "dd/MMM/yy h:mm a"
SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT)
String CurrentDate = new java.sql.Timestamp(new Date().getTime()).format("dd/MMM/yy h:mm a")
Date date = formatter.parse(CurrentDate)
//log.error 'CurrentDate: ' + CurrentDate

SimpleDateFormat sdfAmerica = new SimpleDateFormat(DATE_FORMAT)
TimeZone tzInAmerica = TimeZone.getTimeZone("America/Los_Angeles")
sdfAmerica.setTimeZone(tzInAmerica)

String sDateInAmerica = sdfAmerica.format(date)
Date dateInAmerica = formatter.parse(sDateInAmerica)

//log.error 'INAMERICA000: ' + formatter.format(dateInAmerica)
cfHOTReason.setReadOnly(false)
cfHOTReason.setRequired(true)
cfHOTDateOn.setFormValue(formatter.format(dateInAmerica))

}

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events