Forums

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

Create a calculated number script field in Scriptrunner based on the value from each single select f

jack January 24, 2023

I need to realize the calculation of the following fields through scriptrunner
issues has 5 fields
Single choice field A (2023, 2024...).
Single choice field B (office a, office b...).
Numeric field C (total amount for office a, total amount for office b...).
Numeric field D (Amount).
Numeric field E (office a remaining amount, office b remaining amount).

i need to choose
Single choice field A (2023) + Single choice field B (office a, office b...)
The value of field B is automatically obtained (the total amount of office a, the total amount of office b...).
Fill in field C (amount) every time you create a new issue
Total amount of office a - amount (all issues associated with office a) = remaining amount of office a.
Restrictions: remaining amount <0, no issues are allowed

I don't know if the description is clear, please help me solve this problem.Thanks!
I need to run in 2 projects

2 answers

0 votes
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
January 25, 2023

Hi @jack

I have a doubt to clarify, i.e. in your description, you mentioned:-

The value of field B is automatically obtained (the total amount of office a, the total amount of office b...).

I assume this is for Numeric Field C; how do you get the updated value for each year? Is it via DB Query or a REST server, or is it expected to be updated by the Behaviour, i.e. hard coded?

Kindly clarify so I can provide a better example.

Thank you and Kind regards,
Ram

jack January 25, 2023

Hi Ram 

thanks for your reply.

Numeric Field C is basically a fixed value, I hope that it can be filled in manually when writing the script.

Ram Kumar Aravindakshan _Adaptavist_
Community Champion
January 27, 2023

Hi @jack

Below is an example code for your reference:-

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue

def issue = issue as MutableIssue

def customFieldManager = ComponentAccessor.customFieldManager

def totalAllocation = customFieldManager.getCustomFieldObjectsByName('Total Allocation').first()
def totalAllocationValue = issue.getCustomFieldValue(totalAllocation) as Double

def amountUsed = customFieldManager.getCustomFieldObjectsByName('Amount Used').first()
def amountUsedValue = issue.getCustomFieldValue(amountUsed) as Double

if (totalAllocationValue && amountUsedValue) {
(totalAllocationValue - amountUsedValue) as Double
}

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

So this code, basically does a simple subtraction of the Total Allocatoin Value with the Amount Used and will return the Remaining Amount.

Just another doubt to clarify, if let say you do a calculation on one issue for one office, e.g. Office A. And if you create another issue for Office A, are you expecting that the total value remaining is passed on to second issue?

Thank you and Kind regards,
Ram

jack January 27, 2023

Hi @Ram Kumar Aravindakshan _Adaptavist_ thank you for your reply.

yes, expecting that the total value remaining is passed on to second issue

Ram Kumar Aravindakshan _Adaptavist_
Community Champion
January 30, 2023

Hi @jack

Thanks for the clarifications.

If this is the approach you want to take, I'm sorry, but it will not work as expected.

It is a very complex approach. Why? Because the value of the field is not stored anywhere, and if you try to pass it from one issue to another, it will not work as expected.

Unless you want to create a table in the database to store these values so you can query them.

It may be possible (no assurance) if you create an epic and store its value. So every time a new issue is created and added to the epic, the epic can do the calculation, and the latest value from the epic can be passed to the new issue.

I am looking forward to your feedback.

Thank you and Kind regards,

Ram

0 votes
jack January 24, 2023

Hi Ram can you help me? @Ram Kumar Aravindakshan _Adaptavist_

Suggest an answer

Log in or Sign up to answer