Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,637,594
Community Members
 
Community Events
196
Community Groups

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

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 Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Oct 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())

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 Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Oct 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

Thanks @Mohamed Benziane I will test and give feedback!

@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? 

@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...

Mohamed Benziane
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Nov 08, 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 Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Nov 09, 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