Using Script Runner under Behaviours End date should be auto populated

Linisha Jesh May 7, 2021

Hello,

In need help in script runner. I have 3 date fields such as start date, duration and end date, the end date should auto populated in view screen based on start date + Duration provide in create screen. Please help me with right code to get this output with help of script runner under behaviours.

 

Thanks,

Linijesh

1 answer

0 votes
Vikrant Yadav
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 7, 2021

Hi @Linisha Jesh  Instead of behaviour it is better to use scripted “End Date” field. so that once you create the issue, system calculate sum and Put in end date Scripted field. Try below code replace between by addTo 

Replace Lower Date field and higher date field with Start date and duration respectively.

import com.atlassian.jira.component.ComponentAccessor

import java.sql.Timestamp
import java.time.temporal.ChronoUnit

// Get the required component
def customFieldManager = ComponentAccessor.customFieldManager

// The name of the lower date custom field
final String lowerDateCustomFieldName = "Lower Date Custom Field"
// The name of the higher date custom field
final String higherDateCustomFieldName = "Higher Date Custom Field"

// Get the custom field objects
def lowerDateCustomField = customFieldManager.getCustomFieldObjects(issue).find { it.name == lowerDateCustomFieldName }
def higherDateCustomField = customFieldManager.getCustomFieldObjects(issue).find { it.name == higherDateCustomFieldName }
if (!lowerDateCustomField || !higherDateCustomField) {
log.info "Could not find one ore more of the provided custom fields"
return null
}

// Get the date values from both issues
def lowerDateValue = issue.getCustomFieldValue(lowerDateCustomField) as Timestamp
def higherDateValue = issue.getCustomFieldValue(higherDateCustomField) as Timestamp
// Transform both values to instants
def lowerDateInstant = lowerDateValue?.toInstant()
def higherDateInstant = higherDateValue?.toInstant()

// Change the chrono unit to obtain the difference in other time unit.
final chronoUnit = ChronoUnit.DAYS
// Calculate the difference between the lower and the higher date.
lowerDateInstant && higherDateInstant ? chronoUnit.between(lowerDateInstant, higherDateInstant) : null

 

Source of code : https://library.adaptavist.com/entity/calculate-the-difference-between-two-dates

Linisha Jesh May 7, 2021

Hi Vikrant Yadav,

I have already writen script for start date in the transistion to add 20 min to the current time. now i have to fetch this start time + duration to end time with help of behaviour. Can you please help with this..

Thank you,

Regards,

Linijesh

Vikrant Yadav
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 7, 2021

Hi @Linisha Jesh  you want to update end date field value after a transition? As per the tags it seems that you are using Jira Cloud. In jira cloud behaviour is not available. In cloud You can do It via post function or via listener or use scripted end date field. 

Linisha Jesh May 7, 2021

In our organization we are using data center for which i need help

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
FREE
PERMISSIONS LEVEL
Site Admin
TAGS
AUG Leaders

Atlassian Community Events