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.
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!
@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()
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Jeroen Poismans Thank you for answer, but i get error :
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Jeroen Poismans Hi!
Thank you very much for your help! Your code helped a lot! I was able to figure it out
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
This is off topic but have you tried Atlassian Automation? this can be accomplished much easier there and it is well documented.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.