Scripted Field Custom Template

I want to have a custom template in a script field which does some calculation. Basically the script outputs the $value in seconds and I'm able to use the form "$getPrettyDuration.call($value)" (as found in the documentation). But I'd like to convert the seconds into hours, but not changing $value from seconds into hour, just for displaying.

Is there a reference what can be done in this "Custom Template" field? Is this just Groovy script? Is there more documentation available for this field?

  • Version of JIRA: 7.1.9
  • What version of ScriptRunner are you using? 4.3.5

2 answers

1 accepted

0 vote

The templates are for telling JIRA how to handle the data that is in them.  They're not for doing the formatting of the output.

Your scripted field should be outputting $value in the format you want to see it, not trying to format it on the way out.  If you really do need the $value in seconds as well, you'll want a separate field to do that, as the content of the field is different.

Not sure about that, the example in the ScriptRunner documentation (https://scriptrunner.adaptavist.com/latest/jira/recipes/scriptfields/workRemainingInLinkedIssues.html) also does this using

$getPrettyDuration.call($value)

When saving it as a number instead of free text I think indexing of the field makes more sense, also when using this field in searches for sorting it's easier when it's saved as number. Or am I wrong here?

Yes, it outputs the value that is desired.

You're right about sorting, you want numeric for that.  The problem here is that you're asking for two values to come out of one thing.  For that to work, you need code inside jira to do it - your script could, in theory, output both, but JIRA would need to understand "show value 1, while search/sorting on value 2".  The original estimate field is built that way, but ScriptRunner only has templates for single value outputs - text OR numbers OR dates etc.

That said, might be worth asking Adaptavist to add a new template for "length of time" - store seconds, display pretty.  I will poke the team and see if we've already got an issue raised for it.

Thanks Nic for clarification. The idea with the two values won't be too bad. But what's against doing it like the function getPrettyDuration? Something like an output formater? Are there any other functions available like getPrettyDuration?

There's nothing against using the pretty duration thing, it works fine.  The problem is that you're trying to work with two values in one field.

I don't really need two values. Let me rephrase my original question:

  • Save one value (here in seconds)
  • Format the output (here in hours)

The getPrettyDuration is not configurable (afaik), that means it always outputs it in the "pretty" format designed by Atlassian, which isn't what I want. All I want is to just convert the seconds stored in the index in hours for display purposes.

One questions stays: What functions are available for Custom Templates, besides the getPrettyDuration? Is there anywhere a documentation available?

Then you build a custom field that has an output of <value field>/3600 (maybe with some rounding)

 

Ok, that would make it... So do I understand this correctly that there are no more functions available for Custom Templates than getPrettyDuration?

1 vote

I'll take a risk here and disagree with Nic. 

Templates are precisely for formatting the value returned by the script field code for display to use the user.

You can convert to hours using code as shown below. 

Caution: You must use the number range indexer for the field, otherwise ScriptRunner will convert the return value to whatever is suitable for the chosen indexer.

image2016-8-30 10:30:50.png

(for detail see https://answers.atlassian.com/download/attachments/40348731/image2016-8-30%2010%3A30%3A50.png?version=1&modificationDate=1472549439634&api=v2)

Ok, that would make it... So do I understand this correctly that there are no more functions available for Custom Templates than getPrettyDuration

There is a bunch of other stuff available although not well documented.

There is a bunch of other stuff available although not well documented.

II have the same question as Tobias. Is there any documentation for what methods are available for custom templates? I've been scouring the internet to no avail,

Methods look to be formatted as Velocity templates but I can't find documentation anywhere.

Yes, they are velocity templates. I tried to answer your question here: https://scriptrunner.adaptavist.com/latest/jira/scripted-fields.html#comment-2928282368

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Sunday in Agility

You asked for it, so we delivered: images on issues have arrived

A picture tells a thousand words. And agility boards have just released their latest feature: cover images on issues – so now your board can tell a story at first glance. Upload attachmen...

165 views 1 10
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you