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!
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
Dear @Sachin Dhamale
My apologies; I believe my question wasn't clear. Could you please post the script for the following details?
<image removed>!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
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.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Dear Piyush,
Could you please share the code for example, as I'm new to groovy?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
}
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.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.