Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

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,555,977
Community Members
 
Community Events
184
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
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Apr 28, 2022

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!

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 04, 2022

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
AUG Leaders

Atlassian Community Events