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?
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.Issuedef 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!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I have a few observations to make for your scripted field script.
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.
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.