On a recent project, one of our developers came across the need to change the display of SLAs in JIRA Service Desk (JSD) from the hh:mm (hour) format to displaying days.
Why, you might ask, would a user want this type of display?
For most users, hours are, more often than not, perfectly suitable. In this case it was simply that the client was used to working with days as their default unit. And when you consider that SLAs can get very long, the day format can be preferable to hours (not least because it saves having to divide everything by 24...)
It seems like a pretty simple piece of functionality, but JSD doesn’t actually offer the option of changing from hours to days. (There's an enhancement request here, but it hasn’t yet been implemented). So, our developer found a solution.
Without knowing exactly how SLAs were calculated in JSD, it was assumed JSD kept the SLA value as hh:mm in its custom field.
This should have been easy: just grab the custom field value and convert to days by using a JIRA Misc Custom Fields calculated custom field (issue.get("customfield_xxxxx") / 24) = win!
Unfortunately, it wasn't that simple. When the value from from JSD's SLA custom field is extracted, all you get is a JSD class and a hash. This hash value is calculated by JSD and only rendered to become hh:mm when displayed with a JSD SLA custom field. In short, there wasn’t anything to manipulate the format in the back end. This left it to front end manipulation (and, as a result, a bit of a hack where there will likely be circumstances where it might not work).
To do this, the Message Custom Field found in the JIRA Toolkit Plugin was used so that AJS (Atlassian's take on JQuery) can be added to a web page. This is similar to how an Announcement Banner works but instead of being global it will only render the AJS for screens where you’ve added this Message Custom Field. This means you can have different AJS for different screens.
All that remained was to create an AJS script to grab the hh:ss value from what was being displayed by JSD, perform some string manipulation to convert into days then display that back on the issue view overwriting the original values.
From there, it was fairly straightforward. After locating the page elements (DOM) displaying the hh:mm value, a loop was used to find every single instance (there can be multiple SLAs). The conversion was performed - something along the lines of (mm/60 + hh)/24 - and the existing hh:mm value was overwritten in the page element. Your JSD should now be displaying values in days.
You'll find the code below...
// Version 1.0 // Can be placed in either the Announcement Banner to apply for the whole Jira instance // or placed in a Message Custom Edit field for specific project