Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Copy custom field date value to other custom field date on create and edit screens

Venkatesh Pettem July 15, 2024

Hello Team,

There are two custom date fields Start date and Baseline start date. whenever the user select the Baseline start date and if Start date is empty then copy the Baseline start date value to start date.

here is my behavior server-side script

import java.text.SimpleDateFormat
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

def baseLineStartDate = getFieldById(fieldChanged)


def startDate = getFieldByName("Start date")
def startDateValue = startDate.getValue() as Date
def baseLineStartDateValue = baseLineStartDate.value as Date


def simpleDateFormat = new SimpleDateFormat('yyyy-MM-dd')

if(startDateValue == null){
    if(baseLineStartDate.value){
        def startDateFormatted = simpleDateFormat.format(startDateValue)
        def baseLineStartDateFormatted = simpleDateFormat.format(baseLineStartDateValue)

        def message = """${baseLineStartDateFormatted}"""
        log.debug("message is" + message)
        startDate.setFormValue(message)
    }  
}


 Please help to correct the Behaviour script





2 answers

2 accepted

0 votes
Answer accepted
Ram Kumar Aravindakshan _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.
July 15, 2024

Hi @Venkatesh Pettem

Welcome to the Community!

I can confirm that your approach will not work as expected.

If you intend to check to verify if the first date field has a value or passes the value of the second date field, you must create two separate Server-Side Behaviours, i.e. for each date field separately for it to work as expected.

Below are the sample working Server-Side Behaviour codes for your reference:-

1. For the Sample Date field:-

import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

import java.text.SimpleDateFormat

@BaseScript FieldBehaviours behaviours
def sampleDate = getFieldById(fieldChanged)
def baselineDate = getFieldByName('Baseline Date')
def simpleDateFormat = new SimpleDateFormat('d/MMM/yy')

if (sampleDate.value && baselineDate.value) {
return
} else if (!sampleDate.value && baselineDate.value) {
sampleDate.setFormValue(simpleDateFormat.format(baselineDate.value as Date))
}

Below is a screenshot of the Server-Side Behaviour configuration for the Sample Date field:-

sample_date_config.png

 

2. For the Baseline Date field:-

import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

import java.text.SimpleDateFormat

@BaseScript FieldBehaviours behaviours
def sampleDate = getFieldByName('Sample Date')
def baselineDate = getFieldById(fieldChanged)
def simpleDateFormat = new SimpleDateFormat('d/MMM/yy')

if (sampleDate.value && baselineDate.value) {
return
} else if (!sampleDate.value && baselineDate.value) {
sampleDate.setFormValue(simpleDateFormat.format(baselineDate.value as Date))
}

Below is a screenshot of the Server-Side Behaviour configuration for the Baseline Date field:-

baseline_date_config.png

Please note that the sample codes provided above are not 100% exact to your environment. Hence, you must modify it accordingly.

 

I hope this helps to solve your question. :-)

Thank you and Kind regards,
Ram

 

0 votes
Answer accepted
Zuri Suwru
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.
July 15, 2024

Hey there,

If I understand your situation correctly, both fields are date fields. In such cases, there really is no need to overcomplicate things by formatting the value as they can just be passed along.

Grabbing the form value object and then setting it for the field in need works perfectly:

 

def targetStartField = getFieldById("customfield_1234")
def targetEndField = getFieldById("customfield_1235")

def targetStartObj = targetStartField.getFormValue()
def targetEndObj = targetEndField.getFormValue()

if(!targetEndObj) {
targetEndField.setFormValue(targetStartObj)
}

Let me know if you still require help with your behavior!

Best regards,
Zuri S.

Suggest an answer

Log in or Sign up to answer