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
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.
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
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:-
3. If the Number 1 field is updated to 150, as shown in the screenshot below:-
4. As expected, the Total field will once again update its value accordingly, as shown in the screenshot below:-
I hope this helps to answer your question. :-)
Thank you and Kind regards,
Ram
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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:-
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:-
2. When the Rate is set to Bad and the Impact is set to low the Priority is set to Medium.
I hope this helps to answer your question. :-)
Thank you and Kind regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Has your question been answered?
If yes, please accept the solution.
Thank you and Kind regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
thx for your time.
i wrote the code in the field impact:
and the field rate:
but it doesnt work. priority dont change.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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:-
Once you have selected the Behaviour, the screens should display the following:-
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.