Copy custom field date value to other custom field date on create and edit screens
Asked by Venkatesh Pettem:

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)

<br />

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

<br />

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)  
}  
}

<br />

Please help to correct the Behaviour script

<br />

<br />

<br />

<br />


Answers:
author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@Venkatesh Pettem](/t5/user/viewprofilepage/user-id/5541453)

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](https://community.atlassian.com/t5/image/serverpage/image-id/335641iBEFEBB6A30D163A5/image-size/large?v=v2&px=999 "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](https://community.atlassian.com/t5/image/serverpage/image-id/335643iBA64E526343E8F3D/image-size/large?v=v2&px=999 "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


author: Zuri Suwru
body: 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.


Tags: data-center

groovy

jira-data-center

scriptrunner