Forums

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

Calculate and update own field based on another custom field.

Ashok Barik
Contributor
March 24, 2022

I am new to script runner, please help me on below requirement. 

 

I have two fields, Field1 (worked hours) and Field2 (total hours). Field2 is noneditable and I want to display the calculation result on it. 
If I create an issue without specifying the number in the field1, then field2 will have the value "0" 
After I update the hours' value in field1, the value of field2 should be "the current value of field2" + "the changed value of field1". It should work both in addition and subtraction (if I enter a negative value in the field1). 
The result should be like this;
Field2 = Field2 (previous value) + Field1 

1 answer

1 vote
Craig Nodwell
Community Champion
March 24, 2022

Scriptrunner has great documentation and recipe examples.
Scriptrunner docs Scripted Fields 

What you might find even more helpful is the library.
Scriptrunner Library 

There's actually a great example that should get you started in the library here:
calculate-custom-field-on-issue-update 

 

Hope this helps.

Ashok Barik
Contributor
March 24, 2022

Hi @Craig Nodwell

Thank you for providing all the details.  

Here is my script, but it gives some random value in field2 (worked total hours). Could you please check it and modify it if I have missed something. 

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.user.ApplicationUser

def issueManager = ComponentAccessor.getIssueManager()
def linkManager = ComponentAccessor.getIssueLinkManager()
def cfm = ComponentAccessor.getCustomFieldManager()

Issue issueKey = issue
def id=issueKey.getId()

def customFieldManager = ComponentAccessor.getCustomFieldManager();

def cf = customFieldManager.getCustomFieldObject("customfield_18849")
def cff = customFieldManager.getCustomFieldObject("customfield_18844")

def n = Integer.parseInt((issue.getCustomFieldValue(cf) ?: 0).toString().replaceAll(~/[.].*/, ""))
def b = Integer.parseInt((issue.getCustomFieldValue(cff) ?: 0).toString().replaceAll(~/[.].*/, ""))

Integer sum = (n ?: 0) + (b ?: 0);

Craig Nodwell
Community Champion
March 24, 2022

So, that's the code for calculating 2 custom fields and populating that value into a 3rd field(a scripted field, this is the code for that scripted field).  

Where are you using this code?

Ashok Barik
Contributor
March 24, 2022

Hi, 

I am using this code in the scripted custom field2 (customfield_18849) and populating the calculation value in this field2 only. 

field1 is editable but field2 is noneditable.

I need the calculated value of field1 and field2 to be populated in field2. 

Ashok Barik
Contributor
March 25, 2022

@Craig Nodwell could you please help me on this? 

Like Craig Nodwell likes this
Craig Nodwell
Community Champion
March 25, 2022

@Ashok Barik I will just a bit busy today, going into a meeting.  Please have a bit of patience.

Ashok Barik
Contributor
March 26, 2022

@Craig Nodwell Thank you. I am just waiting for your response.   

In case my previous question wasn't entirely clear, I would like to repeat my request once more.

 

For example:
I have two fields: field1 (Money spent) and field2 (Total spent). If I add a number in  field1 then it should sum up with the Total Spent field value and then display the result in the Total spent field. 

Suggest an answer

Log in or Sign up to answer