Forums

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

How to write a condition script to compare two different date

Kris Han
Contributor
January 30, 2020

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
Antoine Berry
Community Champion
January 30, 2020

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

Kris Han
Contributor
January 31, 2020

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 Champion
January 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. :)

Kris Han
Contributor
January 31, 2020

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 Champion
January 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
}
Kris Han
Contributor
January 31, 2020

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

Kris Han
Contributor
February 2, 2020

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 Champion
February 3, 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 ?

Kris Han
Contributor
February 3, 2020

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 Champion
February 3, 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.

Kris Han
Contributor
February 3, 2020

Hi @Antoine Berry ,

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

Antoine Berry
Community Champion
February 3, 2020

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

Kris Han
Contributor
February 5, 2020

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 Champion
February 6, 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