Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,299,398
Community Members
 
Community Events
165
Community Groups

How to pull Logged Hours (Time Spent) into custom field then multiply it by 2.10 for every hour

Hello,

I've been researching how we can pull Logged hours (Time Spent) from Time Tracking into a scripted custom field that would then calculate the hours multiplying by 2.10 to display a new total for the SDA Hours custom field.

Reason: We have two different billable price ranges based on hours spent so every hour logged in the Jira ticket for our Project workflow is worth 2.10 hours in SDA Hours

i.e.:

Logged Hours  = 1

SDA Hours = 2.10

I've tried different options from here and other sources and managed to display the Logged hours in the custom field but not calculated. Every article I read was for calculating multiple fields into one custom field whereas here we're just trying to pull data and calculate it into a field that is really just displaying the total calculated result.

Basic script I'm working off for our SDA Hours custom field-

def timeSpent = issue.getTimeSpent();
if (timeSpent == null) {
return null;
}
return timeSpent;

So the question is this even possible? And if so, how would I go about scripting this?

1 answer

1 accepted

0 votes
Answer accepted

Have you not tried to just output the result of your calculation?


if(issue.timeSpent){
return issue.timeSpent * 2.1 as Long
}

Make sure you select "Duration" in the script field template.

Maybe you were missing the "as Long" to eliminate any fractions of seconds as a result of the multiplication.

Thank you for your response. 

I used that in my scripted field. One more question that ties into this scripted field-

If I am using this with a Script Fragment will adding to the Scripted Field prevent the Script Fragment panel from displaying in other Projects?

In my testing in our staging environment it seems to be working that way so long as I have the if (issue.projectObject.key == 'PRODSD') snippet but if I remove it the Scripted Fragment shows up in other projects.

Here is the script residing inside of my Scripted Field-

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

def timeSpent = issue.getTimeSpent();
if (issue.projectObject.key == 'PRODSD')
if (issue.timeSpent){
return issue.timeSpent * 2.1 as Long
}
return timeSpent;

And this is the script I am using for my Script Fragment-

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

 

String TimeHHMM (def timeInSeconds)
{

timeInSeconds = (long) timeInSeconds
def hoursRest = (timeInSeconds / (3600))
def hours = hoursRest as Integer
def minutesRest = ((timeInSeconds % 3600)/60)
def minutes = minutesRest as Integer
def valueToShow = " " + (hours as Integer).toString() + " hours " + (minutes as Integer).toString() + " minutes "
return valueToShow
}

def issue = context.issue as Issue
def html_table = "<table class='aui'><thead><tr><th>Time (Converted)</th></tr></thead><tbody>"
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cfModule = customFieldManager.getCustomFieldObjects(issue).find {it.name == "SDA Hours"}
def valueToShow = "None"
if (issue.getCustomFieldValue(cfModule))
{
valueToShow = TimeHHMM(issue.getCustomFieldValue(cfModule))
html_table = html_table + "</td><td>" + valueToShow + "</td></tr>"
}
html_table = html_table + "</tbody></table>"
writer.write(html_table)

Thanks again for your assistance!

I have a few observations to make for your scripted field script.

  • You don't need any imports
  • You don't need this line: def timeSpent = issue.getTimeSpent();

If you just want to show this calculated field for a project, it might be best to go set that in the custom field context and/or set that custom field to hidden in the other project field configs.

But if you still want to make it exclusive to a project in the code, I would do it this way:

if (issue.projectObject.key == 'PRODSD' && issue.timeSpent){
     return issue.timeSpent * 2.1 as Long
}
return null

Nex, if you select "Duration" in your script field "Template" option, you don't need to do any conversion. It will automatically display as "x hours, y minutes".

But if you have a separate requirement for a fragment to display this value, then I would recommend you investigate JiraDurationUtils and MarkupBuilder

Your script could then be done this way:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.util.JiraDurationUtils
import groovy.xml.MarkupBuilder

def jiraDurationUtils = ComponentAccessor.getComponentOfType(JiraDurationUtils)
def userLocale = ComponentAccessor.jiraAuthenticationContext.locale

def issue = context.issue as Issue

def customFieldManager = ComponentAccessor.customFieldManager
def cfModule = customFieldManager.getCustomFieldObjects(issue).find { it.name == "SDA Hours" }
def duration = issue.getCustomFieldValue(cfModule) as Long //returns seconds
def formattedDuration = jiraDurationUtils.getFormattedDuration(duration, userLocale)
def builder = new MarkupBuilder(writer)
builder.table(class: 'aui') {
thead { tr { th "Time (Converted)" } }
tbody { tr { td formattedDuration } }
}

(the writer is implicitly set as the output of the web panel fragment, and the builder will have written to it along the way)

Your script fragment should have a condition block. If you want to only display the fragment for a certain project, you have to set that up in the condition.

Thank you so much for your help!

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Confluence

An update on Confluence Cloud customer feedback – June 2022

Hi everyone, We’re always looking at how to improve Confluence and customer feedback plays an important role in making sure we're investing in the areas that will bring the most value to the most c...

276 views 2 5
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you