Forums

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

Dynamically change set value of string custom field

Priti Sonar June 13, 2022

Hello , 

I have 2 field .

1) Drop Down Field : With some values , A,B,C.

2) Simple Text Field (Single Line).

I have all 3 screens same for a issue type (view/create/edit).

Now I want whenever I create the ticket.

create.PNG

Based on drop down Value It must set/change the text field value dynamically . Not after Pressing the create button .

I have create script post function in create transition.

 post_function.PNG

 

Here is my code written .

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def country = customFieldManager.getCustomFieldObjectsByName('Bayer Lead Country')[0];
def currency = customFieldManager.getCustomFieldObjectsByName('Billing Currency')[0];

//def myTestField = customFieldManager.getCustomFieldObject(10024);

def countryvalue = issue.getCustomFieldValue(country)

log.warn(countryvalue)

if(countryvalue == "A") {
log.warn('if loop')
issue.setCustomFieldValue(currency,'EUR')
}
else if(countryvalue == "B") {
log.warn('china loop')
issue.setCustomFieldValue(currency,'CNY')
}
else{
log.warn('error')
issue.setCustomFieldValue(currency,'error')
}.

 

Please help me to acheive it dynamically over the create screen .

Also this code Always goes to else and set the value  as error after clicking on create button 

2 answers

1 accepted

0 votes
Answer accepted
Andrea Pannitti
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.
June 13, 2022

Hi @Priti Sonar,

in my instance I have two fields: "Country" (select list) and "Currency" (text field). So, the behaviour code is the following:

import com.atlassian.jira.component.ComponentAccessor

def fldCurrency = getFieldByName("Currency")
String strCountry = ComponentAccessor.optionsManager.findByOptionId(getFieldByName("Country").formValue as Long)

switch (strCountry) {
case "China":
fldCurrency.setFormValue("CNY")
break
case "Europe":
fldCurrency.setFormValue("EUR")
break
case "United States":
fldCurrency.setFormValue("US")
break
default:
fldCurrency.setFormValue("")
}

 On Initialiser (optional) and on Field: Country.

Priti Sonar June 13, 2022

Thank you so much , It is working as expected 

Andrea Pannitti
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.
June 14, 2022

Hi @Priti Sonar ,

if this solution is ok for you, please could you accept my answer?

Thank you

0 votes
Andrea Pannitti
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.
June 13, 2022

Hi @Priti Sonar,

if you want set the field every time an item is selected from the drop-down menu, you must use a behavior and not a postfunction.
The postfunction is triggered only following a state transition (on creation from null to first workflow state).
Also, the code for the behavior is a bit different than that for a postfunction.

Priti Sonar June 13, 2022

Hi @Andrea Pannitti ,

Thanks for this info .

Could you please let me know code snippet according to the requirement .

It will dynamically set the field correct ?

Priti Sonar June 13, 2022

What change of code you are  talking about

Suggest an answer

Log in or Sign up to answer