Scripted Field Custom Template

Tobias Brunner (Admin Account) August 15, 2016

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 votes
Answer accepted
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 15, 2016

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.

Tobias Brunner (Admin Account) August 15, 2016

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?

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 15, 2016

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.

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 15, 2016

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.

Tobias Brunner (Admin Account) August 15, 2016

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?

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 15, 2016

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.

Tobias Brunner (Admin Account) August 15, 2016

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?

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 15, 2016

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

 

Tobias Brunner (Admin Account) August 15, 2016

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

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 15, 2016

Yes

Tobias Brunner (Admin Account) August 15, 2016

Ok, thanks

1 vote
JamieA
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.
August 29, 2016

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.

randybishop September 30, 2016

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.

JamieA
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.
October 1, 2016

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