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

Jira Groovy how to substract days from given date

Hi,

 

I have a target date, which I needs to reduce 2 days during the transition. How I can do it using postfunction.

My target date 10/1/2019 so I would like to set 9/29/2019.

 

I am getting the value of target date by

def TD = customFieldManager.getCustomFieldObjectByName("Target Date")
def TDV = issue.getCustomFieldValue(TD)

but unable to subtract it. How I can do that? Any advise.

Thanks,

Om

1 answer

try something like this, iirc you can just subtract a number from timestamp and groovy will understand

last piece is for updating issue, though if it's a postfunction last line is not necessary

import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.component.ComponentAccessor

def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def TD = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Target Date")
def TDV = issue.getCustomFieldValue(TD)
TDV -= 2
issue.setCustomFieldValue(TD, TDV)
ComponentAccessor.issueManager.unpdateIssue(currentUser, issue, EventDispatchOption.ISSUE_UPDATED, false)

@Ilya TurovIt doesn't work but I did by converting to date and string as below.

 

import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.component.ComponentAccessor

def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser


def TD = customFieldManager.getCustomFieldObjectByName("Target Date")
def TDV = issue.getCustomFieldValue(TD)

Date newTDV = (Date)TDV
def newDateformat = newTDV.format('MM/dd/yyyy')
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");

cal.setTime(sdf.parse(newDateformat));
cal.add(Calendar.DATE, -2);

Timestamp DefTDD = new Timestamp(cal.getTimeInMillis())

issue.setCustomFieldValue(TD, DefTDD)
issueManager.updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED ,false)


I made a typo, should be 

issueManager.updateIssue(...

not unpdateIssue, but anyway, if this postfunction is first in list (before the one that stores issue to db) you don't need to manually update it

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted in Jira

Demo Den Ep. 7: New Jira Cloud Reports

Learn how to use two new reports for next-gen projects in Jira Cloud:  Cumulative flow diagram and Sprint burndown chart. Ivan Teong, Product Manager, Jira Software, demos the Cumulative ...

237 views 1 2
Join discussion

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