Forums

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

Divide one Number field by another number field and add output to another field

Vikrant Yadav
Community Champion
May 17, 2020

Hi Friends, 

 

Hope you all are safe and in good health

I have one query, user is asking for calculation field,

would it be possible to add field to JIRA, containing formula/live calculation?

I have 2 number field Revenue and Time, user want to divide Revenue by Time and put the output in another field name "Profit". Is it possible, please help.

@Stephen_Wright  @Nic_Brough__Adaptavist_  @Kristian Walker (Adaptavist) @Adrian_Stephen @Derek_Fields @Ravi_Sagar__Adaptavist_ 

@Martin Bayer [MoroSystems, s.r.o.] 

 

Thanks 

Vikrant Yadav

 

1 answer

1 accepted

2 votes
Answer accepted
Nic Brough -Adaptavist-
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 18, 2020

A bit more advanced than you need, but you can just drop the bit about following links - https://library.adaptavist.com/entity/calculate-the-sum-of-all-values-of-a-custom-field-in-linked-issues

Martin Bayer [MoroSystems, s.r.o.]
Community Champion
May 18, 2020

Vikrant, the documentation posted by @Nic Brough -Adaptavist-  should be enough, but if you're not programmer, here is a code you should use (I didn't test it so if there are any exceptions, let me know)

NOTE:  do not forget to fill in revenueCFId and timeCFId properties with real custom field IDs

 

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

def customFieldManager = ComponentAccessor.customFieldManager
def revenueCFId = 111
def timeCFId = 222

def revenueCF = customFieldManager.getCustomFieldObject(revenueCFId)
if (!revenueCF) {
log.debug ("Custom field with ID " + revenueCFId + " not found")
return null
}

def timeCF = customFieldManager.getCustomFieldObject(timeCFId)
if (!timeCF) {
log.debug ("Custom field with ID " + timeCFId + " not found")
return null
}

def revenue = issue.getCustomFieldValue(revenueCF)
def time = issue.getCustomFieldValue(timeCF)

return revenue/time

 

Like # people like this
Vikrant Yadav
Community Champion
May 18, 2020

Hi @Nic Brough -Adaptavist- @Martin Bayer [MoroSystems, s.r.o.]  Thanks you guys for the help. Script is working fine. Providing accurate result.

Thanks a lot :) ..you are the experts. 

Now user is asking to provide result in profit per hour, is it possible to calculate/convert time in hour ? Time field doesn't exists, day and time field is available only. Please suggest if there any solution of this.

 

Thanks Again!

Stay Safe!

Nic Brough -Adaptavist-
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 18, 2020

If you have a date or date/time field, you're going to need to come up with a rule to convert it into a length of time

Vikrant Yadav
Community Champion
May 18, 2020

Hi @Nic Brough -Adaptavist- @Martin Bayer [MoroSystems, s.r.o.]  is there any way to convert or calculate issue created date and resolved date into hours. 

Thanks

Vikrant Yadav
Community Champion
May 18, 2020

Hi @Nic Brough -Adaptavist-  how can i convert it into length of time ? Please suggest, is adaptivist library having any script related to this thing ?

 

Thanks

Vikrant Yadav
Community Champion
May 18, 2020

Hi @Nic Brough -Adaptavist- @Martin Bayer [MoroSystems, s.r.o.]  i want divide Revenue ( number field) by Logged Time field and put value in Profit. Please suggest Logged time.PNG

Martin Bayer [MoroSystems, s.r.o.]
Community Champion
May 18, 2020

Hi, you can get logged time in seconds (I hope I remember unit correctly, but you can test it):

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

def worklogManager = ComponentAccessor.worklogManager
def customFieldManager = ComponentAccessor.customFieldManager
def revenueCFId = 111

def revenueCF = customFieldManager.getCustomFieldObject(revenueCFId)
if (!revenueCF) {
log.debug ("Custom field with ID " + revenueCFId + " not found")
return null
}

def revenue = issue.getCustomFieldValue(revenueCF)

def worklogSeconds = worklogManager.getByIssue(issue)*.getTimeSpent().sum()

return revenue/worklogSeconds

  

Like Vikrant Yadav likes this
Vikrant Yadav
Community Champion
May 18, 2020

Thanks @Martin Bayer [MoroSystems, s.r.o.]  it's working perfectly. :)

Sangmesh February 25, 2021

Hello @Martin Bayer _MoroSystems_ s_r_o__

I am new to Jira, I always wonder where to write this code exactly.

Could you please help me understand where exactly I have to write this script to obtain the result.

Thanks,
Sangmesh

Martin Bayer [MoroSystems, s.r.o.]
Community Champion
February 25, 2021

Hi @Sangmesh , welcome on the community. Could you create new thread with Topic you want to discuss? You can mention me, so I receive notification.

This topic is about calulations with custom fields values, so let's keep it clean :) thank you

Like Vikrant Yadav likes this

Suggest an answer

Log in or Sign up to answer