Set default text value on custom field (multiline field) based on single selection value

Lakshmi S October 5, 2022

Hi Team,

  We have requirement that "When user selects the option "No input parcel or input file provided" from Feedback Term single selection field, it should display with prefilled text "Used when a ticket is received and no parcel, transaction tracker link, or attached input file is provided by the requestor." in the "Details" field (multi line field), and same for other values, etc. I have tried with the below script using script runner behaviors, but the value is not not displaying in the "Details" field. Could you please suggest me on this ?

 

 

def fterm = getFieldByName("Feedback Term")
def details = getFieldById("details")

def ftermvalue = fterm.getValue()

if (ftermvalue == "No input parcel or input file provided")
{
def defaultValue = """

Used when a ticket is received and no parcel, transaction tracker link, or attached input file is provided by the requestor.

""".replaceAll(/ /, '')

if (!underlyingIssue?.description)
{
details.setFormValue(defaultValue)
}

}

2 answers

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.
October 9, 2022

Hi @Lakshmi S

After reviewing your description, I can confirm your approach will not work.

Firstly, when you want to update one field based on the value or option of another field, you must use the Server-Side Behaviour and not the Initialiser.

The Initialiser is to set the default values when the Create / Edit / Transition screens are first loaded, and it will not update if any change is made to a particular field. I suggest you take a look at the ScriptRunner Documentation for more information.

Also, the approach you are using to invoke your custom field is incorrect. When invoking a custom field for the Behaviour, you must either use the getFieldByName() method by specifying the exact name of the field or the getFieldById() method using the customField_xxxx parameter.

From the sample code that you have shared in your description, it seems that you have taken the example code to set the default value for the description field when the create screen is first loaded. Since the description field is a system field, the correct approach to invoke it is using getFieldById('description'). Please note that this approach will only work if it is a System Field

Now, regarding your requirement, as I mentioned earlier, you will need to use the Server-Side Behaviour for the Single-Select List, which will determine the value added to the Multi-Line text Field based on the option selected from the Single-Select List.

So, in this case, because the Behaviour is configured for the Single-Select List when you invoke it, you must use getFieldById(fieldChanged), and when you invoke the Multi-Line text field, you need to use getFieldByName('The Fields Name') or getFieldById('customField_xxxxx')

Below is an example 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 sampleMultiLineText = getFieldByName('Sample Multi-Line Text')

def textBody = ''

if (sampleListValue == null) {
textBody
} else if (sampleListValue == 'Option1') {
textBody = 'Used when a ticket is received and no parcel, transaction tracker link, or attached input file is provided by the requestor.'
} else {
textBody = 'This is a sample output'
}
sampleMultiLineText.setFormValue(textBody)

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

Below is a screenshot of the Behaviour configuration:-

behaviour_config.png

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

1. When the Create dialog is first loaded, and no option has been selected from the List, i.e. Sample List, the Multi-Line Text field, i.e. Sample Multi-Line Text, will not be updated.

test1.png

 

2. When Option1 is selected from the List, the Multi-Line text field is updated accordingly as shown below:-

test2.png

3. When any other option is selected from the List, the Multi-Line text also updates the value accordingly

test3.png

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

Thank you, and Kind regards,

Ram

Lakshmi S October 10, 2022

Thank you @Ram Kumar Aravindakshan _Adaptavist_ . The code is working as expected.

 

image_2022-10-10_110256365.png

1 vote
Florian Bonniec
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 5, 2022

Hi @Lakshmi S 

Not sure it's the only issue but 'details' is for sure not a valid field id so you cannot call 

getFieldById but you should use the id of detail or use getFieldByName.
Regards
Lakshmi S October 5, 2022

Hi @Florian Bonniec ,

 I have tried with those changes, but still not working.

Florian Bonniec
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 5, 2022

Are you sure you are getting the right fields ? the condition is right ?

You can test each step to figure out which one is wrong.

First check if you get the right fields by using this code

def fterm = getFieldByName("Feedback Term")

def details = getFieldByName("details")

/*Test if you get the right fields by setting a description

 that should be displayed under the field in the form if not

 there is an issue getting the field

 */

 fterm.setDescription("Test")

 details.setDescription("Test")

 

If you get Test under both field, check that you are getting the expected value for the field

def fterm = getFieldByName("Feedback Term")

def details = getFieldByName("details")

def ftermvalue = fterm.getValue()

fterm.setDescription(ftermvalue)

if (ftermvalue == "No input parcel or input file provided")

{

def defaultValue = """

Used when a ticket is received and no parcel, transaction tracker link, or attached input file is provided by the requestor.

""".replaceAll(/ /, '')

if (!underlyingIssue?.description)

{

details.setFormValue(defaultValue)

}

}

 

 etc ...

 

In your script why are you checking if the description field is empty before setting a value for detail field ? Are you mixing description and details field ?

 

Regards

 

 

Lakshmi S October 6, 2022

Hi @Florian Bonniec ,

 It won't work on custom field values ? No, I don't want mix up description field here. Please check the below code, there is no issue.

 

image_2022-10-06_133354974.png

Florian Bonniec
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 6, 2022

Having no issue do not means that the script is doing what you want.

It's only check for syntax error.

 

Regards

Suggest an answer

Log in or Sign up to answer