Not Working : Sprint Value is set to value, Fix Version and Release Date fields are mandatory
Asked by Lakshmi S:

Hi Team,

<br />

<br />

<br />

![sprint.png](/t5/image/serverpage/image-id/342003i079EA6D29985CE5D/image-size/large?v=v2&px=999 "sprint.png")

<br />

```
def errorText = "For the Ticket Type Bug, if the Sprint field is set, the Fix Version/s and Release Date are mandatory. "

def sprintField = getFieldByName("Sprint")

def fixField = getFieldByName("Fix Version/s")

def releaseField = getFieldByName("Release Date")

def sprintValue = sprintField.getValue()

if (sprintValue != "") 

{

    releaseField.setRequired(true)

    fixField.setRequired(true)

    releaseField.setError(errorText)

    fixField.setError(errorText)

    

}

   

 else

 {

    releaseField.setRequired(false)

    fixField.setRequired(false)

    

   }
```

I need to ensure that the "fix version" and "release date" fields are mandatory when the "sprint" field is not empty. I've attempted to achieve this using the following ScriptRunner behavior. When we populate the sprint value and provide values for the fix version and release date fields, it's displaying an error. Is there something wrong with this script? This script is commonly used in various scenarios.


Answers:
author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@Lakshmi S](/t5/user/viewprofilepage/user-id/11952)

For your requirement, you must ensure that you create a Server-Side Behaviour configuration for the Sprint field.

Below is a working code for your reference:-  

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

@BaseScript FieldBehaviours behaviours
def storyPoints = getFieldByName('Story Points')
def storyPointsValue = storyPoints.value

def fixedVersions = getFieldById('fixVersions')
def sprint = getFieldByName('Sprint')
def sampleDate = getFieldByName('Sample Date')

fixedVersions.required = false
sprint.required = false
sampleDate.required = false

if (storyPointsValue) {
    fixedVersions.required = true
    sprint.required = true
    sampleDate.required = true
}
```

Please note that the sample working code above is not 100% exact to your environment. Hence, you must make the required modifications.

Below is a screenshot of the Behaviour configuration:-

![behaviour_configuration.png](https://community.atlassian.com/t5/image/serverpage/image-id/342138i76ABAE9D2F0FE35A/image-size/large?v=v2&px=999 "behaviour_configuration.png")

I am also including a couple of test screenshots for your reference:-

1. When the Create screen is first loaded, and no value is set to the Story Points field, the fields are not set to mandatory as shown in the screenshot below:-

![test1.png](https://community.atlassian.com/t5/image/serverpage/image-id/342141i6072DDD5C963EAED/image-size/large?v=v2&px=999 "test1.png")

2. However, if a value is added to the Story Points field, the Fix Version/s, Sprint and Date field is set to required as shown in the screenshot below:-

![test2.png](https://community.atlassian.com/t5/image/serverpage/image-id/342142iDEDAB2CB03E98524/image-size/large?v=v2&px=999 "test2.png")

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

Thank you and Kind regards,  
Ram

comments: author: Lakshmi S
body: Sorry [@Ram Kumar Aravindakshan _Adaptavist_](/t5/user/viewprofilepage/user-id/2988015) for confusing. Its not based on story points, if the "Sprint" field is not empty, the fix version and Release Date are mandatory.  

I used this script, but not working.  

```
def errorText = "For the Ticket Type Bug, if the Sprint field is set, the Fix Version/s and Release Date are mandatory. "

def sprintField = getFieldByName("Sprint")

def fixField = getFieldByName("Fix Version/s")

def releaseField = getFieldByName("Release Date")

def sprintValue = sprintField.getValue()

if (sprintValue != "") 

{

    releaseField.setRequired(true)

    fixField.setRequired(true)

    releaseField.setError(errorText)

    fixField.setError(errorText)

    

}

   

 else

 {

    releaseField.setRequired(false)

    fixField.setRequired(false)

    

   }
```

![sprint.png](https://community.atlassian.com/t5/image/serverpage/image-id/342193iC3CDC6C55729D025/image-size/large?v=v2&px=999 "sprint.png")


author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@Lakshmi S](/t5/user/viewprofilepage/user-id/11952)

Sorry, I misread your description.

So if it's for the Sprint field, the code is pretty much the same, only set the if condition based on the Sprint field's value like below:-  

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

@BaseScript FieldBehaviours behaviours
def sprint = getFieldById(fieldChanged)
def sprintValue = sprint.value

def storyPoints = getFieldByName('Story Points')
def fixedVersions = getFieldById('fixVersions')
def sampleDate = getFieldByName('Sample Date')

fixedVersions.required = false
storyPoints.required = false
sampleDate.required = false

if (sprintValue) {
    fixedVersions.required = true
    storyPoints.required = true
    sampleDate.required = true
}
```

Please note, the sample working code above is not 100% exact to your environment. Hence, you must make the required modifications.

Below is a screenshot of the Behaviour configuration:-

![behaviour_config.png](https://community.atlassian.com/t5/image/serverpage/image-id/342309i5C450F6030115463/image-size/large?v=v2&px=999 "behaviour_config.png")

Below are a couple of test screenshots for your reference:-

1. When the create screen first appears none of the fields are set to required as shown below:-

![test1.png](https://community.atlassian.com/t5/image/serverpage/image-id/342310i50AA46057F925D3A/image-size/large?v=v2&px=999 "test1.png")

2. If the value from the Sprint list is selected, all the other fields will be set to required as shown below:-

![test2.png](https://community.atlassian.com/t5/image/serverpage/image-id/342311i711E77386F2A064F/image-size/large?v=v2&px=999 "test2.png")

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

Thank you and Kind regards,  
Ram


author: Lakshmi S
body: Hi [@Ram Kumar Aravindakshan _Adaptavist_](/t5/user/viewprofilepage/user-id/2988015) ,

Were you able to create the ticket ? its showing mandatory but its not allowing to create a ticket.  

![sprint.png](https://community.atlassian.com/t5/image/serverpage/image-id/342580i9D767305BB00C773/image-size/large?v=v2&px=999 "sprint.png")


author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@Lakshmi S](/t5/user/viewprofilepage/user-id/11952)

I don't seem to be encountering this problem in my environment.

The error message you are getting suggests that you haven't updated the Behaviour code according to your environment.

From the screenshot you shared, the Release Notes field is set as required, but the error message indicates that the Release Dates field is mandatory.

Please double-check if your code is updated according to the fields you are using in your environment and ensure that all the fields that have been set to required have a value so the ticket can be created.

Thank you and Kind regards,

Ram


author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@Lakshmi S](/t5/user/viewprofilepage/user-id/11952)

Has your question been answered?

If yes, please accept the answer provided.

Thank you and Kind regards,

Ram


Tags: behaviours

data-center

jira-data-center

scriptrunner