Fetch the latest date between 2 custom fields in case they're exist

Gilad Shtern December 7, 2022

I'd like to fetch the latest date between 2 custom fields in case they're exist, as followed:

CF1 exit after status "Active"

While, CF2 may or may not exist.

However, script is not working when the CFs is not exist.

I've written the followed script:

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField
import java.text.SimpleDateFormat
import com.atlassian.jira.issue.Issue
import java.util.Date
def LatestDue = new SimpleDateFormat("d/MMM/yy")
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf1 = customFieldManager.getCustomFieldObject(FIELD_ID1)
def cf2 = customFieldManager.getCustomFieldObject(FIELD_ID2)
def DueField = issue.getDueDate()
if(cf1 != null && cf2 != null && issue.getCustomFieldValue(cf2).after(issue.getCustomFieldValue(cf1))){
    return LatestDue = issue.getCustomFieldValue(cf2)
}else if(cf1 != null && cf2 != null && issue.getCustomFieldValue(cf1).after(issue.getCustomFieldValue(cf2))){
    return LatestDue = issue.getCustomFieldValue(cf1)
}else if(cf1 != null){
    return LatestDue = issue.getCustomFieldValue(cf1)
}else{
}
Kindly,
Gilad

1 answer

1 accepted

0 votes
Answer accepted
Tom Lister
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 7, 2022

Hi @Gilad Shtern 

 

What are you intending to return?

 return LatestDue = issue.getCustomFieldValue(cf2)

is saying return the date formatter set equal to a date value which will probably return null

If you want a date string 

return LatestDue.format((Date) issue.getCustomFieldValue(cf2))

the value will need to be cast to a Date. 

You may need to cast to Date for the after function although not so sure without testing that out.

You could also try just using "compare"

issue.getCustomFieldValue(cf2).compareTo(issue.getCustomFieldValue(cf1)) < 0)

DueField doesn't seem to be used here.

Gilad Shtern December 8, 2022

Hi @Tom Lister ,

 

Thank for the clarification.

However, I've not managed to do selective If's.

Meaning, it work good only when both fields exists.

But, in case, it may only CF1 exist.

 

Kindly,

Gilad

Tom Lister
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 8, 2022

Hi @Gilad Shtern 

You code should always return values for cf1 and cf2 as long as the custom field definitions exist.

What won't always be present are the field values on each issue.

pull those out first

def cf1Value  = issue.getCustomFieldValue(cf1)

def cf2Value  = issue.getCustomFieldValue(cf2)

if(cf1Value != null && cf2Value != null && cf2Value.after(cf1Value)){
   

return LatestDue.format((Date) cf2Value)
Gilad Shtern December 8, 2022

@Tom Lister , works.

Thank you!

Tom Lister
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 8, 2022

Great - could you mark the answer as accepted. Thx

Like Gilad Shtern likes this

Suggest an answer

Log in or Sign up to answer