Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to display script field in format "xx D YY h ZZ m QQ s"

Norbert Grzebieniarz
September 26, 2018

Dear Community,

I have a field that calculates the difference between two dates:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.util.*
import java.util.Date.*

def customFieldManager = ComponentAccessor.getCustomFieldManager();
def dateResponse = customFieldManager.getCustomFieldObjectByName('Date of First Response');

if(issue.getCustomFieldValue(dateResponse) && issue.created)
{
def dateValue= issue.getCustomFieldValue(dateResponse) as Date
def dateValue2= issue.created
def dif = dateValue.getTime() - dateValue2.getTime()
return dif
}

I want to display value of this field in format: "XX Days YY Hours ZZ Minutes QQ Seconds"

Anyone can help me out?

Thanks in advance.

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
1 vote
Answer accepted
Thanos Batagiannis [Adaptavist]
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 Champions.
May 25, 2018

Hi Patrice, 

Instead of collecting and iterating through emails why not iterating through the list of the applications users, where each Application user holds the email address and the display name. 

Or even collect in a map the email address (as a key) and the display name (as a value) and the iterate through this map. 

Example code

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.ApplicationUser

def issue = issue as MutableIssue
def users = ComponentAccessor.customFieldManager.getCustomFieldObject("customfield_10100")
def userlist = issue.getCustomFieldValue(users) as List <ApplicationUser>

def emailAndNameMap = userlist?.collectEntries {
[(it.emailAddress) : it.displayName]
}

userlist?.each {
log.debug "Email address $it.emailAddress"
log.debug "Display name $it.displayName"
//do some rest calls
}

One last thing why you use the REST api to create the users and not the Java API ? 

Patrice Marrec (Valiantys)
June 4, 2018

Hi Thanos, 

Many thanks for this and apologies for the delay, was away. 

Part of the 'brief' is that I only have a custom field with email addresses separated by semicolons. Users do not exist yet. I have to grab each email, parse first part and use it as DisplayName and create users on a remote Jira instance... 

Tried your method to map and .collectEntries as it.emailAddress and it.displayName but it fails as it does not know it is an email address 

groovy.lang.MissingPropertyException: No such property: emailAddress for class: java.lang.String

EDIT: I found my original method to be working, I hadn't accounted for trailing white spaces... 

Thanks 

 

Thanos Batagiannis [Adaptavist]
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 Champions.
June 5, 2018

Hi Patrice, 

Oh ok, I got the use case wrong (I thought the custom field was a MultiUserPicker).

Glad that you sorted this out. 

Regards, Thanos

TAGS
AUG Leaders

Atlassian Community Events