Forums

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

Script question for getting current date based on status

Mary Mark July 13, 2023

Hi Atlassian Community, looking for some help to set current date/time based on change of status. 

Here is the sample script but does not seem to be working as expected. 

Start Date and End Dates are both Jira date picker fields

 

import java.time.LocalDateTime

import com.atlassian.jira.issue.status.Status 

def oldStatus = transientVars.oldStatus as Status 

def status = ['To Do', 'Not Started'

if (oldStatus.name in status) { 

issue.set {     

setCustomFieldValue('Start Date', LocalDateTime.now())   

setCustomFieldValue('End Date', LocalDateTime.now()) 

}

}

else {  setCustomFieldValue('End Date', LocalDateTime.now())}

 

1 answer

1 accepted

0 votes
Answer accepted
Florian Bonniec
Community Champion
July 13, 2023

Hi @Mary Mark 

 

You script do not seem right but I may be wrong. If you have a recent version of Scriptrunner you can use HAPI to update the issues.
Reagrds
Mary Mark July 13, 2023

Hi Florian,

I am using scriptrunner. Here's the modified version but still not populating dates. 

 

import com.atlassian.jira.datetime.LocalDate
//import java.time.LocalDateTime
import com.atlassian.jira.issue.status.Status

def oldStatus = transientVars.oldStatus as Status

def status = ['Not Started', 'Triage']

if (oldStatus.name in status) {
  issue.set {
    setCustomFieldValue('Start Date', LocalDate)
    setCustomFieldValue('End Date', LocalDate)
  }
}
else {
  issue.set {
  setCustomFieldValue('End Date', LocalDate)
}
}
Florian Bonniec
Community Champion
July 13, 2023

Are you using a postfunction or a listener ?

Could you not use Automation for JIRA instead ?

Mary Mark July 13, 2023

I am using a postfunction. We don't currently have automation for JIra yet. 

Thank you! 

Mary Mark July 13, 2023

I would love to see how it is done in Automation for Jira. Do you have an example? 

Like John Funk likes this
Florian Bonniec
Community Champion
July 14, 2023

OK if you are using a post-function issue.set should work, you may have error in your script or condition is never true.

 

If you are in a transition from 1 status to another why to you need to validate the previous status ? Also I would have use new Date() instead of LocalDate

 

About automation you could create 2 rule that use the Issue transition trigger, select the 2 status as From value and let Target status to empty. Then you could add an action to edit issue and select Start Date, use {{now}} as value

The second rule will be the same but you let emplty the from transition and the to transition in the trigger configuration. as action you select End Date and add  {{now}} as value.

 

I do not really understant the logic of setting the same date in both field but this should do what the script is doing.

 

Reagrds

Mary Mark July 14, 2023

I have a simple agile workflow with the following statuses: Not Started, Triage, In Progress and Cancelled. All of these are "all to all" statuses. 

When Cancelled status is offered from Not Started and Triage, set the Start Date and End date to current Date

When Cancelled status is offered from In Progress, set the End Date to current date.

That is the reason I'm checking for the old/previous status. 

I tried the following using New Date() putting the code in postfunction but it still does not work as expected. No dates are being populated. 

 

import com.atlassian.jira.issue.status.Status
import java.time.LocalDate

def oldStatus = transientVars.oldStatus as Status

def status = ['Triage', 'Not Started', 'Ready for Team']
if (oldStatus.name in status) {
issue.set {
setCustomFieldValue('Start Date', new Date())
setCustomFieldValue('End Date', new Date())

}
}


else {
issue.set{
setCustomFieldValue('Start Date', new Date())

}

}

Florian Bonniec
Community Champion
July 14, 2023

Hi 

Could you try this script ? It has to be placed at the top of the postfunction list.

 

import java.time.LocalDate
String previousStatus = issue.status.name
def status = ['Triage', 'Not Started', 'Ready for Team']
if (previousStatus in status) {
    issue.set {
        setCustomFieldValue('Start Date', LocalDate.now())
        setCustomFieldValue('End Date', LocalDate.now())
    }
}else {
    issue.set{
        setCustomFieldValue('Start Date', LocalDate.now())
    }
}

Because the post-function is placed before the change are saved in the database, the previous status remain the current one. 

Regards

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events