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

Set system date to custom field(date picker) only if the field is empty [Script Runner]

RSP August 18, 2021

Hi,

There is  “UX Status” dropdown custom field with below status values

  • NA (default)
  • Grooming Needed
  • Ready for Design
  • Ready for Development
  • Ready for UX QA 

There should be  read-only field, to capture the Date when above Status is changed

  • Grooming Needed Date (Date field)
  • Ready for Design Date (Date field)
  • Ready for Development Date (Date field)
  • Ready for UX QA Date (Date field)

I have written behavior script on "UX status" field.  I am trying to only set a custom field to the current date/time (timestamp) if it is currently empty (null). If there is already a value set, no action should occur. 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.properties.APKeys
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import groovy.transform.BaseScript


import java.time.LocalDateTime
import java.time.LocalTime
import java.time.format.DateTimeFormatter

@BaseScript FieldBehaviours fieldBehaviours


def singleSelect = getFieldByName('UX Status')
def singleSelectValue = singleSelect.value
def groomingNeededDateField = getFieldByName('Grooming Needed Date')
def readyForDesignDateField = getFieldByName('Ready for Design Date')
def readyForDevelopmentDateField = getFieldByName('Ready for Development Date')
def readyForUXQADateField = getFieldByName('Ready for UX QA Date')

//Set the appropriate date value, dependent on the value of the currently selected single select option
switch (singleSelectValue) {
// Change 'Single Select Option...' to match your single select's values.

case 'Grooming Needed':

if(groomingNeededDateField ==null){
         setValueToDateField(groomingNeededDateField)
}
break

}

void setValueToDateField( FormField groomingNeededDateField) {

def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

def dateFormatText = ComponentAccessor.applicationProperties.getDefaultBackedString(APKeys.JIRA_DATE_PICKER_JAVA_FORMAT)
def jiraFormatter = ComponentAccessor.getComponent(com.atlassian.jira.datetime.DateTimeFormatter).forUser(currentUser)

def dateFieldFormat = DateTimeFormatter.ofPattern(dateFormatText, jiraFormatter.locale)


def newLocalDateTime = LocalDateTime.now().with(LocalTime.MIN)
groomingNeededDateField.setFormValue(dateFieldFormat.format(newLocalDateTime))

}

As of now I'm trying on groomingNeededDateField but it's not working properly. why so?

1 answer

Suggest an answer

Log in or Sign up to answer
0 votes
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 18, 2021

Rather than try to start debugging this, I'm going to do the consultant thing - take a step back and ask why you are trying to do this the hard way?

A Behaviour will set a field based on user input, and the field it sets will be editable by people, only filled in after people interact with an issue and won't be set if they create or edit in any of the places Behaviours can work (if they use an app or REST call, it won't work)

The data you're trying to get into the fields looks to me to be static calculated data you don't really want the humans to be touching.  So while automating it is a good thing to do, Behaviours is not a good way to do it.

A better way to do it would be with a post-function that calculates the date and puts it into the target fields - this would work on the transitions in your workflow and not need to put anything in front of your users.  It would also work if the transitions are triggered by REST or .  If you wanted to catch Edits, you'd need to do it as a listener instead of a post-function.    And, in both post-functions and listeners, the code would be a lot more simple.

Even better would be a scripted field - it has all the advantages of doing it as a listener, and if you wanted to be really clever with it, you could have it read the issue history for the change of status and calculate from there.  This would enable you to populate the fields for historical issues as well - re-indexing the project after you've got the scripted field working would populate it for all issues, not just ones you create or edit.

RSP August 18, 2021

hi @Nic Brough -Adaptavist- 

I am completely new to this platform and scripting. Will you please tell me what will be the script for post function or listener for above requirement.

RSP August 18, 2021

Hi, 

The "UX status" field is available on transition screen. Depends upon the selection from UX status dropdown status, the other date fields associated with them should get updated.

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 18, 2021

Ok, that's still an automation you should be doing with a post-function or listener, not a front-end action.

https://library.adaptavist.com/entity/update-the-value-of-custom-fields-through-the-script-console shows you how to work with all types of field in a function script.

TAGS
AUG Leaders

Atlassian Community Events