Forums

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

I need the remaining value of two predefined custom filed values.

Ashok Barik
Contributor
March 12, 2022

Hello there,
I am using Jira 8.4.x and need some assistance.

We have predefined the hour of matching value from fields 1 and 2.
For example, if I choose A from field1 and B from field2, the total hour is 60. (There are multiple total hours values according to the field1 and field2)

In my case, I want the remaining hour in the result if someone mentions his work hour in the filed3 then

Example:

Field1=A
Field2=B
(AB total hours = 60)
Field3 (My working hour) = 4
Remaining Hour I want = 60 - 4 = 56hr

 

Thank you in advance!

 

 

2 answers

1 accepted

0 votes
Answer accepted
Sachin Dhamale
Community Champion
March 12, 2022

@Ashok Barik ,

Welcome to the Atlassian Community.

1. First thing you will need script runner plugin for this

2. You need to create scripted field  and bellow script into it. Refer This Doc to know how to add script in scripted field 

3. In that scripted field you need to write script which will take values from field1 field2 and field 3 and will do calculation

= (field1*field2) - field3 and display the value in the scripted field

Here is the script 

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(id1) //field1 ID
def cff = customFieldManager.getCustomFieldObject(id2) // field2 ID
def field3 = customFieldManager.getCustomFieldObject(id3) // field3 ID

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

Integer remaining = (n*b) - c
return remaining

 

Accept the answer if it helps

Ashok Barik
Contributor
March 12, 2022

Dear @Sachin Dhamale 

My apologies; I believe my question wasn't clear. Could you please post the script for the following details?

<image removed>!

Sachin Dhamale
Community Champion
March 12, 2022

@Ashok Barik ,

 

In this case you can use the same script with addition of if condition. Script it bellow

In same way you need to add if condition for other combination.

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(id1) //field1 ID - Category Field
def cff = customFieldManager.getCustomFieldObject(id2) // field2 ID - Size field
def field3 = customFieldManager.getCustomFieldObject(id3) // field3 ID - Actual hour work

def n = issue.getCustomFieldValue(cf).toString()
def b = issue.getCustomFieldValue(cff).toString()
def c = Integer.parseInt((issue.getCustomFieldValue(field3) ?: 0).toString().replaceAll(~/[.].*/, ""))
def d = 0;
if(n=="CONCEPT" && b =="M"){ d = 48 }

Integer remaining = d - c
return remaining
Ashok Barik
Contributor
March 13, 2022

Dear @Sachin Dhamale 

Thank you so much for brightening my day. It works! :) 

Like Sachin Dhamale likes this
1 vote
Piyush A (STR)
Community Champion
March 12, 2022

Use the scripted field in the scriptrunner which would be your result and make it read only. 
kindly review the library of scriptrunner to get the code. 

fetch the values of fields

based on your conditions, do the calculations 

and set the result to yours scripted field

Ashok Barik
Contributor
March 12, 2022

Dear Piyush,
Could you please share the code for example, as I'm new to groovy?

Piyush A (STR)
Community Champion
March 12, 2022

How to add field: https://scriptrunner.adaptavist.com/5.6.8/jira/scripted-fields.html

 

def Field1 = getCustomFieldValue("field name")
//same above for another fields field2, field2
if (/*field values are not null*/) {
def result=//do your calculation
return result 
//if getting failed, try to parse the field values into integer
}
else {
return 0
}
Like Ashok Barik likes this
Ashok Barik
Contributor
March 12, 2022

Thank you. 

Suggest an answer

Log in or Sign up to answer