Make description field mandatory based on select list custom field

Ulrich Kone
Contributor
November 16, 2023

Hello

I'm trying to make the systeme field Description mandatory when a certain custom field get a specific value. If not the case then not

The custom field name is "A catégoriser"

Here is the script I did

 

import com.atlassian.jira.issue.fields.DescriptionSystemField
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

//Apply Scriptrunner behaviour on select list field

def CustomField = getFieldById('customFieldId=11200') //select list field
def desc = getFieldByName('description') //multi-line text field

log.debug('customFieldId=11200' + CustomField.getValue())

if(CustomField.getValue() == "A catégoriser") {
   DescriptionSystemField.setRequired(true)
} else {
    DescriptionSystemField.setRequired(false)
}
But the script failed
Can somebody help me in that?
Best regards

1 answer

1 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.
November 16, 2023

Hi @Ulrich Kone

After going through the sample code that you have shared, I can confirm that the approach you are taking is incorrect.

To proceed, please confirm what Behaviour configuration you are using for this. Is it a Server-Side Behaviour or the Initialiser? If it is the former, i.e. Server-Side Behaviour for the Single-Select List, then it is the correct approach. However, if it is the latter, the approach is wrong.

From your code, I assume you are trying to use a Server-Side Behaviour for the Single-Select List, whereby the Description field is set to mandatory based on the option selected on the Single-Select List.

For this to work, you must make a couple of corrections. Firstly, your approach to declaring the Single-Select list is incorrect. Instead of declaring it as:-

def CustomField = getFieldById('customFieldId=11200')

Instead, you must declare it as:-

def CustomField = getFieldById(fieldChanged)

You must use the fieldChanged value to ensure that the Behaviour will only trigger when the change has been made to the Single-Select List. Otherwise, the Behaviour will not work correctly.

Next, your declaration of the Description field is also incorrect. In your code, you have declared it as:-

def desc = getFieldByName('description'

This will not work. Using the lowercase d in Description is meant for the Description field's ID, not the name.

Hence, you must declare it as:-

def desc = getFieldById('description')

Below is a fully working code for your reference:-

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

@BaseScript FieldBehaviours behaviours
def sampleList = getFieldById(fieldChanged)
def sampleListValue = sampleList.value.toString()

def description = getFieldById('description')
description.required = false

if (sampleListValue == 'Option1') {
description.required = true
}

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

Below is a screenshot of the Server-Side Behaviour configuration:-

behaviour_config.png

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

1. When the Create dialog first loads and no option has been selected from the Single-Select List as expected, the Description field is not set to mandatory as shown in the screenshot below:-

test1.png

 

2. If Option1 is selected from the Single-Select List, as expected, the Description field is set to required as shown in the screenshot below:-

test2.png

3. If any other option aside from Option1 is selected, the Description field will not be set to required as shown in the screenshots below:-

test3.png

test4.png

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

Thank you and Kind regards,

Ram

 

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.
November 20, 2023

Hi @Ulrich Kone

Has your question been answered?  If yes, kindly accept the solution provided.

Thank you and Kind regards,

Ram

Ulrich Kone
Contributor
November 20, 2023

 

Hello @Ram Kumar Aravindakshan _Adaptavist_ 

Thanks for your asnwer and sorry for the delay

I tried the solution but it seems to not work

Maybe I forgot something

I am inclunding the screenshots

Ulrich Kone
Contributor
November 20, 2023

File 1.PNGFile 2.PNGFile 3.PNGFile 4.PNG

Ulrich Kone
Contributor
November 21, 2023

Hello @Ram Kumar Aravindakshan _Adaptavist_ 

Thank you very much

It works properly (I had a quote in my custom field Value)

Suggest an answer

Log in or Sign up to answer