Forums

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

Using Scriptrunner to compare Date Picker field to Current Date

Thanos
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
September 26, 2024

Hello, I want my workflow to FastTrack to a specific transition if a "date picker" field I have is less than 60 days of Current Date (todays date). 

FastTrack Post Function selected and specific transition to skip to is selected.

I believe my script to properly calculate the date is incorrect below.

Thank you.

 

 

 

def sixtyDaysAgo = new Date().minus(60)
 
cfValues['datepickerfield'] < sixtyDaysAgo

2 answers

1 vote
xelomax October 1, 2024

Hello!

It seems that the date calculation has a small error. You can use this script in ScriptRunner to compare the field of type "datepicker" with the current date. The script checks if the field date is less than 60 days from the current date and automatically transitions

I hope it serves as a guide for you

import java.time.LocalDate
import java.time.format.DateTimeFormatter
import groovy.json.JsonSlurper
def issueKey = "TEST-1"
def issueResponse = get("/rest/api/3/issue/${issueKey}?fields=customfield_10015")
    .header('Content-Type', 'application/json')
    .asString()

 

if (issueResponse.status == 200) {
    def issueData = new JsonSlurper().parseText(issueResponse.body)
    def datePickerField = issueData.fields.customfield_10015

 

    // Define the current date and date formatter
    def currentDate = LocalDate.now()
    def formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")

 

    // Parse the date from the Date Picker field
    if (datePickerField) {
        def parsedDate = LocalDate.parse(datePickerField as String, formatter)
        def daysDifference = java.time.temporal.ChronoUnit.DAYS.between(parsedDate, currentDate)

 

        // Check if the date is less than 60 days from today
        if (daysDifference <= 60) {
            logger.info("The date is within 60 days. Transitioning issue...")

 

            // Perform the transition
            def transitionResponse = post("/rest/api/3/issue/${issueKey}/transitions")
                .header('Content-Type', 'application/json')
                .body([
                    transition: [
                        id: "21" // Replace with the ID of the transition you want to perform
                    ]
                ])
                .asString()
            if (transitionResponse.status == 204) {
                logger.info("Transition successful.")
            } else {
                logger.error("Failed to transition the issue. Response: ${transitionResponse.status}")
            }
        } else {
            logger.info("The date is more than 60 days ago. No transition performed.")
        }
    } else {
        logger.error("Date Picker field is empty.")
    }
} else {
    logger.error("Failed to retrieve issue details. Response: ${issueResponse.status}")
}

1 vote
Valerie Knapp
Community Champion
September 27, 2024

Hi @Jonathan Grizaniuk , welcome to the Atlassian Community and thanks for your question.

Adaptavist have made available a library of different scripts where, what I would typically do, is go and look for something similar to my use case and then adapt what they have shared.

I think this one might help you - https://library.adaptavist.com/entity/transition-search-issues?tab=cloud 

Please take a look and share your feedback.

Cheers

Suggest an answer

Log in or Sign up to answer