Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,460,928
Community Members
 
Community Events
176
Community Groups

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

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 Dec 07, 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.

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 Dec 08, 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)
Tom Lister Community Leader Dec 08, 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