Forums

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

How do i get Jira Custom Field Value in a Script Runner

VINAY KUMAR GOUNI May 22, 2018

 

I have a Script which gets the value of DueDate (Jira Default Field) and calculates the value out from Present Date and.

********************************************************************

import com.atlassian.jira.issue.Issue
import groovy.time.TimeCategory
import groovy.time.TimeDuration
import java.text.SimpleDateFormat;

// Get a pointer to the current issue and extract the Due Date as a string

Issue issue = issue

Date dueDate = issue.getDueDate()

// Get the current date and time
Date today = new Date()

// Work out the number of days between the current date and the due date
def daysBetween = dueDate - today

*********************************************************************

Need some help updating this code to extract a Custom Field (Date Value) instead of Default DueDate Field.

Basically, how do i extract a Custom field (A Date Type) Value out?

Trying to replace DueDate (Default Field) with DateTypeField (Custom Field).

 

1 answer

0 votes
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 Leaders.
May 25, 2018

Hey Vinay 

Should be something like

def cfName = "First DateTime"

//get the custom field with name First DateTime
def cf = ComponentAccessor.customFieldManager.getCustomFieldObjects(issue)?.find { it.name == cfName }

if (! cf) {
log.debug "Custom field with name $cfName doesn't exist"
return
}

// get the value of the custom field
def dateValue = issue.getCustomFieldValue(cf) as Timestamp

Date today = new Date().toTimestamp()
def daysDiff = today - dateValue

log.debug daysDiff
VINAY KUMAR GOUNI May 30, 2018

Hi Thanos,

 

Thank you for the input,

I have the following error while trying to executing the provided code:

Groovy.jpeg

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 Leaders.
May 30, 2018

The timestamp import is missing, add the following in your existing imports

import java.sql.Timestamp
VINAY KUMAR GOUNI May 30, 2018

Hi Thanos,

It Worked, it also made me add Component Accessor Import.

But, The Results display null for the Preview.

Not Sure the Differences in the Date Types is causing this issue or something else

 

Date Type.jpeg

For the case above it should display a Result: 84 instead it is displaying null.

Requested_DueDate(06/15/18) - Created (03/22/18) = 84

 

Any thoughts/Advises.

 

Thanks

Vinay Kumar

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events