How to add 5 days to a date?(scriptrunner)
Asked by Alex:

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]
```

<br />

Any help is important.

Best regards, Alex.

<br />


Answers:
author: Jeroen Poismans
body: 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!

comments: author: Alex
body: [@Jeroen Poismans](/t5/user/viewprofilepage/user-id/1708479) 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()
```


author: Jeroen Poismans
body: 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


author: Alex
body: [@Jeroen Poismans](/t5/user/viewprofilepage/user-id/1708479) Thank you for answer, but i get error :

![error.png](https://community.atlassian.com/t5/image/serverpage/image-id/301784iE572CB6F77F62259/image-size/large?v=v2&px=999 "error.png")


author: Alex
body: 1. [@Jeroen Poismans](/t5/user/viewprofilepage/user-id/1708479) you don't know there could be a problem?


author: Alex
body: [@Jeroen Poismans](/t5/user/viewprofilepage/user-id/1708479) Hi!

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


author: Alex
body: 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)
```


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


Tags: jira-service-desk-server

jira-service-management

jira-service-management-server