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}*.destinationObjectif (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.
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!
final version of the code :
import com.atlassian.jira.event.type.EventDispatchOptionimport com.atlassian.jira.component.ComponentAccessorimport com.atlassian.jira.issue.Issuedef 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 = targetEndcalendar.add(Calendar.DAY_OF_MONTH, 5)issue.setCustomFieldValue(cfTargetEnd, calendar.time) ComponentAccessor.issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
This is off topic but have you tried Atlassian Automation? this can be accomplished much easier there and it is well documented.
@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.ComponentAccessorimport com.atlassian.jira.issue.Issuedef issue = Issues.getByKey("PROJECT-4198")def customFieldManager = ComponentAccessor.getCustomFieldManager()int daysToAdd = 10def FirstDate = customFieldManager.getCustomFieldObjectByName("Target end")def dateAValue = issue.getCustomFieldValue(FirstDate) as Datedef Calculated = Calendar.getInstance()Calculated.setTime(dateAValue)Calculated.add(Calendar.DATE, daysToAdd)return Calculated.getTime()
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
@Jeroen Poismans Thank you for answer, but i get error :
It looks like you're new here. Sign in or register to get started.