Little help with script runner please

Hi, I've been trying to get script runner to update a field using Script Listeners.
I've got it somehow working, but I can't figure out this entirely

I have the following fields:
 - Today:  (date, this gets updated via script listeners)
 - BIrthDate: (date)
 - Age (Single line text)

I'd like for Age field to contain "X months, X years old" by calculating the difference between Today and BirthDate fields.

So far I've managed to get Today to contain the right date.

def projectKey = 'TP'

// Vars
def today = '{customfield_11213}'
def age = '{customfield_11212}'
def bdate = '{customfield_11200}'

def tdate = new Date() 

def years =  (age - tdate)
def months = (age - tdate)

def strdiff = """ ${months} months, ${years} years """

def message = """ Vals today ${today}, age ${age}, bdate ${bdate}, tdate ${tdate}, years ${years}, months ${months}"""

logger.info("${message}")

put("/rest/api/2/issue/${issue.key}") 
    //.queryString("overrideScreenSecurity", Boolean.TRUE) 
    .header("Content-Type", "application/json")
    .body([
        fields:[
                (today): tdate,
                (age): strdiff
        ]
    ])
    .asString()

I'm using JIRA Cloud, and the latest version of the plugin.

 

1 answer

1 accepted

Hi Fernando,

This code should work:

import java.time.LocalDate

// Ignore the Issue Update event if it isn't for the right project
if (issue.fields.project.key != 'VIS') {
    return
}

// These are the custom field references
def todayCf = 'customfield_11213'
def bdateCf = 'customfield_11200'
def ageCf = 'customfield_11212'

// Extract the existing values from the issue
def birthdayField = issue.fields[bdateCf] as String
if (birthdayField == null) {
    // No birthday was specified, we can't calculate the age
    return
}
def ageField = issue.fields[ageCf] as String

// We should use LocalDate or ZonedDateTime instead of Date
def tdate = LocalDate.now()
def bdate = LocalDate.parse(birthdayField)

// Calculate age in years and months
def years = tdate.getYear() - bdate.getYear()
def months = tdate.getMonthValue() - bdate.getMonthValue()

def age = "${months} months, ${years} years"
def message = "Vals tdate: ${tdate.toString()}, bdate: ${bdate.toString()}, age: ${age}"

logger.info(message)

// Only update the issue if the age text has changed
if (age != ageField) {
    put("/rest/api/2/issue/${issue.key}") 
        //.queryString("overrideScreenSecurity", Boolean.TRUE) 
        .header("Content-Type", "application/json")
        .body([
            fields:[
                (todayCf): tdate.toString(),
                (ageCf): age
            ]
        ])
        .asString()    
}

Thanks,
Jon

Great! Thank you so much!

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

85 views 0 5
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you