Forums

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

How to use ScriptRunner to update Issue Due Date, Target Start and Target End with adding 12 months

Patrick Kosche October 18, 2022

Hi guys,

I am trying to update the due date with a groovy script in ScriptRunner. I have found different solutions on the community but it doesn't work for me. 

I just want to select issues based on a jql search an update the dates fields (due date, target start and target end) of all issues with the existing value +12months.

Can you help me?

Thanks!

Patrick 

1 answer

0 votes
Mohamed Benziane
Community Champion
October 20, 2022

Hi,

I use this code to add a couple of days to a date customfield

 

def myCustomfieldValue = myCustomfield.getValue(issue)
Calendar
calendar = Calendar.getInstance();

calendar.setTime(new Date ( myCustomfieldValue.getTime()))

int days = 10;

while (days > 0){

    calendar.add(Calendar.DAY_OF_YEAR, 1);

    if(calendar.get(Calendar.DAY_OF_WEEK) <= 5)--days;

}

def newDate= new Timestamp (calendar.getTimeInMillis())

Patrick Kosche October 20, 2022

Hi @Mohamed Benziane thanks a lot for your answer. Which classes do you import and how do you write the new date back to the customfield?

Do you have an idea how to select the issues based on a jql query?

Does this work for standard fields like due date too?

Sorry for the questions, I am a newbee :)

 

Thanks a lot!

Patrick

Mohamed Benziane
Community Champion
October 20, 2022

Here a full sample (please test it and let me know if it's ok)

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


def issueManager = ComponentAccessor.getIssueManager()
def issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey(<MyIssueKey>)

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def customfield = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(<MyCustomfieldID>)
def customfieldValue = customfield.getValue(issue)


calendar calendar = Calendar.getInstance();
calendar.setTime(new Date ( customfieldValue.getTime()))
int days = 10;
while (days > 0){
calendar.add(Calendar.DAY_OF_YEAR, 1);
if(calendar.get(Calendar.DAY_OF_WEEK) <= 5)--days;
}
def newDateVlue = new Timestamp (calendar.getTimeInMillis())

issue.setCustomFieldValue(customfield,newDateVlue)
issueManager.updateIssue(currentUser,issue,EventDispatchOption.DO_NOT_DISPATCH ,false)

You will need to add a jql request a go throught each issue to update the value you want.

https://scriptrunner.adaptavist.com/5.6.8/jira/recipes/misc/running-a-jql-query.html

Patrick Kosche October 20, 2022

Thanks @Mohamed Benziane I will test and give feedback!

Patrick Kosche November 8, 2022

@Mohamed Benziane I tested a lot in addition with the libary of Adaptavist Scriptrunner.

My solutions for a test (+20days to due date) looks like:


import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter
import java.sql.Timestamp

def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchService = ComponentAccessor.getComponent(SearchService)
def issueManager = ComponentAccessor.getIssueManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

// query
def query = jqlQueryParser.parseQuery("project = TK and assignee = currentUser()")

def search = searchService.search(user, query, PagerFilter.getUnlimitedFilter())

log.debug("Total issues: ${search.total}")

search.results.each { documentIssue ->
log.debug(documentIssue.key)

// mutable issue:
def issue = issueManager.getIssueObject(documentIssue.id)

// change date
issue.setDueDate(new Timestamp((new Date() + 20).time))
log.debug(issue.dueDate)
}


but that doesn't work. The query works, i get the correct issues, but the field update won't work. I have no clue why...

I also tried just to update the description:

issue.setDescription("TEST PKE")

instead of

issue.setDueDate(new Timestamp((new Date() + 20).time))

 

without result.

 

Can you help? 

Patrick Kosche November 8, 2022

@Mohamed Benziane  I found a solution with an actual date:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter
import java.sql.Timestamp
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.UpdateIssueRequest

def versionManager = ComponentAccessor.getVersionManager()
def projectComponentManager = ComponentAccessor.getProjectComponentManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def userUtil = ComponentAccessor.getUserUtil()


def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchService = ComponentAccessor.getComponent(SearchService)
def issueManager = ComponentAccessor.getIssueManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

// edit this query to suit
def query = jqlQueryParser.parseQuery("project = TK and assignee = currentUser()")

def search = searchService.search(user, query, PagerFilter.getUnlimitedFilter())


log.warn("Total issues: ${search.total}")

search.results.each { documentIssue ->
log.warn(documentIssue.key)

// generate mutable issue:
def issue = issueManager.getIssueObject(documentIssue.id)

// update dates...
log.warn(issue.dueDate)
issue.setDueDate( new Timestamp((new Date() + 1).time))
log.warn(issue.summary)
log.warn(issue.dueDate)
issueManager.updateIssue(user, issue, UpdateIssueRequest.builder().build())
}

 

now I will try to read the existing date value and add the days...

Patrick Kosche November 8, 2022

@Mohamed Benziane  Thanks a lot!!

It is now running for standard field duedate,

 

I am struggling with the Custom date fields, how can I seperate the day, month and year out of a custom date field in jira?

Mohamed Benziane
Community Champion
November 9, 2022

Hi Patrick,

You can get the value from the customfield date like this :

 

def customfield = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(Your customfieldId) 

def cfValue = customfield.getValue(issue)

log.warn(cfValue)

The value that you will get is the type of java.sql.Timestamp so to get the month your can do something like this

//Create a new calendar
Calendar
calendar = Calendar.getInstance()
//set calendar with the CF date value

calendar.setTime(new Date ( cfValue.getTime()))

//YEAR
log.warn(calendar.get(Calendar.YEAR))

//MONTH
log.warn(calendar.get(Calendar.MONTH)+1)//first month is 0

//DAY
log.warn(calendar.get(Calendar.DAY_OF_MONTH))

 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events