code for calculated custom field depending of other fields

gonzalo zegarra
Contributor
May 3, 2023

hi 

i have two customfields: A and B

I need a new calculated field that shows me values ​​depending on the values ​​I choose from the two previous fields A and B.

what would be the code that i need in the calculated field?

 

Thx

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
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.
May 3, 2023

Hi @gonzalo zegarra

For your requirement, I would suggest using ScriptRunner's Custom Scripted Field.

Also, I recommend upgrading your ScriptRunner plugin to the latest release, i.e. 8.2.0 as there have been many new features along with bug fixes added.

scripted_field_config.png

Below is a working sample code for your reference:-

def number1 = issue.getCustomFieldValue('Number 1') as Long

def number2 = issue.getCustomFieldValue('Number 2') as Long

if (!number1 && !number2) {
0 as Long
} else if (!number1) {
number2 as Long
} else if (!number2) {
number1 as Long
} else {
(number1 + number2) as Long
}

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

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

1. First, when the issue is being created, some values are entered into the Number 1 and Number 2 fields

test1.png

2. Once the issue is created, as expected, the Total field will appear, displaying sum total of values in field Number 1 and field Number 2, totalling to 300, as shown in the screenshot below:-

test2.png

3. If the Number 1 field is updated to 150, as shown in the screenshot below:-

test3.png

4. As expected, the Total field will once again update its value accordingly, as shown in the screenshot below:-

test5.png

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

Thank you and Kind regards,

Ram

gonzalo zegarra
Contributor
May 7, 2023

thx in understand  it, instead of long what type of variable would go? because the values ​​I work with are from a text type selection list

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.
May 7, 2023

Hi @gonzalo zegarra

Could you please clarify what types are used for the options in the List? Are they String types?

If they are string types adding them together will not work as expected because it will only merge the values together.

If you want to extract the numbers from the List, you could try something like:-

def list = issue.getCustomFieldValue('List 1')

int value = Integer.parseInt(list.replaceAll("[^0-9]", ""))

Following which you will need to convert the value to a Long / Integer following the steps that  I have provided in my previous comment, i.e.

def list1 = issue.getCustomFieldValue('List 1')
int number1 = Long.parseLong(list1.replaceAll("[^0-9]", ""))

def list2 = issue.getCustomFieldValue('List 2')
int number2 = Long.parseLong(list2.replaceAll("[^0-9]", ""))


if (!number1 && !number2) {
0 as Long
} else if (!number1) {
number2 as Long
} else if (!number2) {
number1 as Long
} else {
(number1 + number2) as Long
}

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

Thank you and Kind regards,

Ram

gonzalo zegarra
Contributor
May 8, 2023

thx for your time. i have two fields that i created:

impact (customfield_12021) that contains values (good, bad) and the other field is rate (customfield_12022) that contains ( low, fast).

for example, if i choose impact as good and rate is low, the priority field of the issue have to change to High. 

or if i choose impact as bad and  rate is fast, the priority field of the issue have to be Normal.

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.
May 8, 2023

Hi @gonzalo zegarra

Your last comment is totally different from your description.

So basically there is no actual calculation required; it is pretty much just setting the value of the priority field correct?

If yes, then you can do it just using the Behaviour itself.

For this, you will need to create two Server-Side Behaviour configurations. The first will be for the Impact list and the second will be for the Rate list.

Below is the Server-Side Behaviour code for the Impact list:-

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

@BaseScript FieldBehaviours behaviours
def impact = getFieldById(fieldChanged)
def impactValue = impact.value as String

def rate = getFieldByName('Rate')
def rateValue = rate.value as String

def priority = getFieldById('priority')

if (impactValue == 'Good' && rateValue == 'Low') {
priority.setFormValue('High')
} else if (impactValue == 'Bad' && rateValue == 'Fast') {
priority.setFormValue('Medium')
}

Below is the Server-Side Behaviour code for the Rate List:-

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

@BaseScript FieldBehaviours behaviours
def rate = getFieldById(fieldChanged)
def rateValue = rate.value as String

def impact = getFieldByName('Impact')
def impactValue = impact.value as String

def priority = getFieldById('priority')

if (impactValue == 'Good' && rateValue == 'Low') {
priority.setFormValue('High')
} else if (impactValue == 'Bad' && rateValue == 'Fast') {
priority.setFormValue('Medium')
}

 Please note that the sample working codes provided are 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 Impact list is set to Good and the Rate list is set to Low the Priority is set to High as shown in the screenshot below:-

test1.png

 

2. When the Rate is set to Bad and the Impact is set to low the Priority is set to Medium.test2.png

 

I hope this helps to answer 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.
May 10, 2023

Hi  @gonzalo zegarra

Has your question been answered?

If yes, please accept the solution.

Thank you and Kind regards,

Ram

gonzalo zegarra
Contributor
May 11, 2023

thx for your time.

i wrote the code in the field impact:

image.png

and the field rate:

image.png

but it doesnt work. priority dont change.

image.png

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.
May 11, 2023

Hi @gonzalo zegarra

From the latest screenshot you shared, I can confirm that what you are trying will not work. Why is it so?

The main reason is that you have not added the codes correctly to ScriptRunner's Behaviour. Instead, you are adding it to some other third-party plugin.

Hence the failure of the Behavior to work as expected.

To use ScriptRunner's Behaviour, first, you will need to the ScriptRunner tab and select the Behaviour feature as shown in the screenshot below:-

image_2023-05-12_130446492.png

Once you have selected the Behaviour, the screens should display the following:-

behaviour_config2.png

You should then click the Create the Behaviour button add the Projects that you want the Behavior to work for, and add the Server-Side Behaviour code I provided in my previous comment.

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.
May 17, 2023

Hi @gonzalo zegarra

Just to check, were you able to set the Behaviour code accordingly and get it to work?

Has your question been answered? If yes, please accept this answer.

Thank you and Kind regards,

Ram

gonzalo zegarra
Contributor
May 19, 2023

sorry, i dont have that pluggin :(

TAGS
AUG Leaders

Atlassian Community Events