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

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How to write a condition script to compare two different date

Hi all,
As title, I don't know how to write condition which can compare two different date. Can someone provide a simple script for me? Thank you very much!

 

Best Regards,

 

1 answer

1 accepted

0 votes
Answer accepted

Hi @Kris_Han ,

With this script you can compare two date fields in a condition :

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()

int dateField1Id = 12500
int dateField2Id = 12501

def dateField1 = customFieldManager.getCustomFieldObject(dateField1Id)
def dateField2 = customFieldManager.getCustomFieldObject(dateField2Id)

def dateField1Value = issue.getCustomFieldValue(dateField1)
def dateField2Value = issue.getCustomFieldValue(dateField2)

if (dateField1Value.compareTo(dateField2Value)){
passesCondition = true
}
else {
passesCondition = false
}

Hope that helps.

Antoine

Hi @Antoine Berry ,
Thank you very much! One more quick question, how do I check the datefieldID? Can I use xpath instead of field Id? Thank you very much again.

 

Kris

Antoine Berry Community Leader Jan 31, 2020

The best practice is to use the id so the script will keep working even if you change the name. You can find the id in the url when you are on the edit page of the custom field. :)

Hi @Antoine Berry ,

Sorry to brother you again, I tried the script but it did not work. I used it on the copy value of post-function and it always did copy value action either the date filed 1 bigger than date filed 2 or date field 2 bigger than date filed 1. Could you help me to figure it out? Thank you very much!

 

Kris

Antoine Berry Community Leader Jan 31, 2020

What is your requirement ? A condition disables a transition, so you should not even see the button in one case. Maybe add logs and check the logs 

if (dateField1Value.compareTo(dateField2Value)){
log.error("condition is true")
passesCondition = true
}
else {
log.error("condition is false")
passesCondition = false
}

Hi @Antoine Berry ,

I want use "Copy value from field to filed" post function and need to set a condition for it. So, it should only copy value from field to field when the sprint end date bigger than my another date time filed. However, I tried this script but it did copy value all the time. Please help me to fix it. Thank you very much!  Btw, I add logs but I don't know how to find it. 

 

Best Regards,

Kris

Hi @Antoine Berry ,

I fixed it and run smoothly now. Really thanks for your help. Could I ask one more question? Do you know how to get epic link's sprint end date by using script? Thank you very much!

 

Best Regards,

Kris 

Antoine Berry Community Leader Feb 03, 2020

Glad you could make it work !

I am not sure about your question : Epics typically run through multiple sprints so they are not linked to one specific sprint. Am I missing something ?

Hi @Antoine Berry ,

Actually, I am trying to get the sprint from the epic which is in “epic link” field and only need to current sprint of that epic. Could we make it? Thank you very much! 

Kris

Antoine Berry Community Leader Feb 03, 2020

Well I find it weird that you associate an Epic with a sprint, since epics are typically completed in multiple sprints and are not displayed in the backlog. 

But still it is possible using that script : 

import com.atlassian.jira.component.ComponentAccessor

int epicLinkId = 10101
def epicLink = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(epicLinkId)
def epicLinkValue = issue.getCustomFieldValue(epicLink)

int sprintFieldId = 10100
def sprintField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(sprintFieldId)
def sprintEndDateValue = issue.getCustomFieldValue(sprintField)?.endDate?.first()?.toDate()

Let me know if that helped.

Hi @Antoine Berry ,

Thank you very much! I did some modification and finally make it : ) 

Antoine Berry Community Leader Feb 03, 2020

You are very welcome ! Glad you could make it work. :)

Hi @Antoine Berry ,

Sorry to brother you again, do you know how to write a script to determine which sprint is "active sprint" and which is "future sprint"? Thank you very much!

 

Kris

Antoine Berry Community Leader Feb 06, 2020

If you want to know if the sprint associated with the issue is active, closed or future you can use : 

def sprint = issue.getCustomFieldValue(sprintField)[0]
boolean isActive = sprint.isActive()
boolean isClosed = sprint.isClosed()
boolean isFuture = sprint.isFuture()

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you