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
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.
Below is a working sample code for your reference:-
def number1 = issue.getCustomFieldValue('Number 1') as Longdef number2 = issue.getCustomFieldValue('Number 2') as Longif (!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
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:-
issue.getCustomFieldValue('List 1')<span>int</span> <span>value</span> <span>=</span> Integer.parseInt(list.replaceAll(<span>"[^0-9]"</span>, <span>""</span>))<br><span>def list = </span>
<span>int</span> <span>value</span> <span>=</span> Integer.parseInt(list.replaceAll(<span>"[^0-9]"</span>, <span>""</span>))<br>
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.
issue.getCustomFieldValue('List 1')<span>int</span> <span>number1</span> <span>=</span> Long.parseLong(list1.replaceAll(<span>"[^0-9]"</span>, <span>""</span>))<br><span>def list2 = </span>issue.getCustomFieldValue('List 2')<span>int</span> <span>number2</span> <span>=</span> Long.parseLong(list2.replaceAll(<span>"[^0-9]"</span>, <span>""</span>)) <br>if (!number1 && !number2) { 0 as Long} else if (!number1) { number2 as Long} else if (!number2) { number1 as Long} else { (number1 + number2) as Long}<span>def list1 = </span>
<span>int</span> <span>number1</span> <span>=</span> Long.parseLong(list1.replaceAll(<span>"[^0-9]"</span>, <span>""</span>))<br>
<span>def list2 = </span>
<span>int</span> <span>number2</span> <span>=</span> Long.parseLong(list2.replaceAll(<span>"[^0-9]"</span>, <span>""</span>)) <br>
I hope this helps to answer your question. :-)Thank you and Kind regards,
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.
It looks like you're new here. Sign in or register to get started.