Populate custom date field with release date of the version selected in custom version type field

Neetika Kumar August 21, 2018

Custom Fields:
Iteration: Version type Single select field - displays list of all versions for the selected project.
Production Date: Date type

Requirement: Need to populate 'Production Date' with the release date of the version selected in the 'Iteration' field.

Note:
-> We are using a custom field for selecting the version as we want to restrict single version selection on the issue.
-> We cannot take 'Production Date' as a scripted field as we want the date to be input by user in case of a particular version of 'Off Cycle'
      
We are trying out the Behaviours functionality to achieve the same and are stuck at a point where jira crashes when the behaviour executes.

The below code executes without error and we are getting the selected version from 'Iteration' field in temp.

import com.atlassian.jira.component.ComponentAccessor
def versionManager = ComponentAccessor.getVersionManager()
def versions = versionManager.getVersions(issueContext.projectObject)

def itr = getFieldByName("Iteration")
def prod = getFieldByName("Production Date")

def abc = itr.getValue() as String
def temp = abc.substring(abc.lastIndexOf("[")+1,abc.lastIndexOf("]"))

We now want to read the entire version list (versions) and compare each version name with 'temp' to find the version whose Release date needs to be fetched and put in Production Date.

We appended the below code to see how versions can be read and JIRA crashed.
def xyz = getFieldByName("Test Field")  //'Test Field' is a text type field
xyz.setFormValue(versions[0])

Please guide us how this can be achieved. We are thinking of somewhat like below but unable to try out as cannot risk JIRA to be down again.

for(i in versions)
    {
    def name = i.getName()
    if(temp == name && temp !="Off Cycle")
      {
      def release = i.getReleaseDate()
      prod.setFormValue(release)
      }
    }

We are in the process of JIRA license purchase and the  above requirement is critical.

Thanks!

1 answer

1 accepted

1 vote
Answer accepted
Mark Markov
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 21, 2018

Hello @Neetika Kumar

Yes, the problem happens because you ve tried to set Version object in text field.

Your suggestions are correct. Another variation of script

import com.atlassian.jira.component.ComponentAccessor


def versionManager = ComponentAccessor.getVersionManager()
def versions = versionManager.getVersions(issueContext.projectObject)

def itr = getFieldByName("Iteration")
def prod = getFieldByName("Production Date")

def abc = itr.getValue() as String
log.error("Iteration value: ${abc}")
def temp = abc.substring(abc.lastIndexOf("[")+1,abc.lastIndexOf("]"))
log.error("Iteration value cleaned: ${temp}")

def version = versions.find {it.name == temp && temp != "Off cycle"}
if (version){
def releaseDate = version.getReleaseDate()
prod.setFormValue(releaseDate)
}

 And it is good practice to log variables. That allows you see what values variables contains in atlassian-jira.log while your script executes.

Neetika Kumar August 21, 2018

Thanks Mark. The script worked great.

Just one addition I did to the script to populate date instead of date time in Production Date.

def version = versions.find {it.name == temp && temp != "Off cycle"}
if (version){
    def releaseDate = version.getReleaseDate()
    String newDate = releaseDate.format("dd/MMM/yy")
    prod.setFormValue(newDate)
}

 

Thanks!

rae edwards October 31, 2019

We are trying to auto populate a custom field. e.g. product version, based on the customer's organization and value selected in another custom field e.g. product.

We want the product version field to be auto-populated with the appropriate value before the service desk agent views the ticket in JSD.

Will the above proposed solution work and does this require a plugin?  

Suggest an answer

Log in or Sign up to answer