Forums

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

How to add 5 days to a date?(scriptrunner)

Alex
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 22, 2024

Hi,everyone!

Please tell me how can I add 5 days to a date field? I have a list of tasks, each of them has a "Target end" field. and you need to add 5 days to the date

My code :

import com.atlassian.jira.issue.link.IssueLink;
import com.atlassian.jira.component.ComponentAccessor;

List issuesFromEpic = []

String trigger_issue = "PROJECT-4198"

while (trigger_issue != null) {

def issue = Issues.getByKey(trigger_issue)
def issueEpicLinkName = ComponentAccessor.getIssueLinkManager().getOutwardLinks(issue.getId())findAll {
it.issueLinkType.outward == "blocked" //== blocked
}*.destinationObject

if
(issueEpicLinkName.size() == 1) {// one blocked link

issuesFromEpic += issueEpicLinkName[0] //PROJECT-4199
trigger_issue = issueEpicLinkName[0]
}
else {
break
}
}

//return issuesFromEpic
//Output >>> [PROJECT-4199, PRESALE-3711, PRESALE-3712, PROJECT-4200]

List targetEnd = []

for (i in issuesFromEpic){

def issueInEpic = Issues.getByKey("${i}").getCustomFieldValue('Target end').format("dd.MM.yy")
targetEnd += issueInEpic

}

return targetEnd

//Output date from issue >>> [27.11.23, 07.12.23, 15.12.23, 22.12.23]

Any help is important.

Best regards, Alex.



3 answers

2 accepted

1 vote
Answer accepted
Jeroen Poismans
Community Champion
January 22, 2024

Hi there,

If that is your only requirement, I suggest Automation. If you still need a script, maybe try the Calendar class:

...
def cfTargetEnd = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Target End")
Date targetEnd = issue.getCustomFieldValue(cfTargetEnd)


Calendar calendar = Calendar.getInstance()
calendar.time = targetEnd
calendar.add(Calendar.DAY_OF_MONTH, 5)

issue.setCustomFieldValue(cfTargetEnd, calendar.time)
ComponentAccessor.issueManager.updateIssue(ComponentAccessor.userManager.getUserByName("admin"), issue, EventDispatchOption.DO_NOT_DISPATCH, false)
...

 

Hope this helps!

Alex
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 22, 2024

@Jeroen Poismans hi!

I have script , and with his help I get the result(in scriptrunner ,in console) I need in the console, but the date is not updated. Do you know what could be wrong with it? Thank you.

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue

def issue = Issues.getByKey("PROJECT-4198")
def customFieldManager = ComponentAccessor.getCustomFieldManager()

int daysToAdd = 10
def FirstDate = customFieldManager.getCustomFieldObjectByName("Target end")
def dateAValue = issue.getCustomFieldValue(FirstDate) as Date
def Calculated = Calendar.getInstance()

Calculated.setTime(dateAValue)
Calculated.add(Calendar.DATE, daysToAdd)

return Calculated.getTime()
Jeroen Poismans
Community Champion
January 22, 2024

 

Hi Alex,

You 're missing code to update the issue:

 

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

...
...

issue.setCustomFieldValue(FirstDate, Calendar.getTime())
ComponentAccessor.issueManager.updateIssue(ComponentAccessor.userManager.getUserByName("admin"), issue, EventDispatchOption.DO_NOT_DISPATCH, false)

 

Give it a try!

Regards

Alex
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 22, 2024

@Jeroen Poismans Thank you for answer, but i get error :

error.png

Alex
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 22, 2024
  1. @Jeroen Poismans you don't know there could be a problem?
Alex
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 23, 2024

@Jeroen Poismans Hi! 

Thank you very much for your help! Your code helped a lot! I was able to figure it out

0 votes
Answer accepted
Alex
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 23, 2024

final version of the code :

 

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

def issue = Issues.getByKey("PROJECT-4198")
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def cfTargetEnd = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Target end")
Date targetEnd = issue.getCustomFieldValue(cfTargetEnd)

Calendar calendar = Calendar.getInstance()
calendar.time = targetEnd
calendar.add(Calendar.DAY_OF_MONTH, 5)

issue.setCustomFieldValue(cfTargetEnd, calendar.time)
ComponentAccessor.issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
0 votes
Adonis ElFakih January 22, 2024

This is off topic but have you tried Atlassian Automation? this can be accomplished much easier there and it is well documented. 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events