I have to update the due date of a subtask based on the custom field called "Launch Date" in the Parent issue. I am trying to test my code:
import com.atlassian.jira.ComponentManager import com.atlassian.jira.issue.CustomFieldManager import com.atlassian.jira.issue.fields.CustomField import com.atlassian.jira.issue.IssueManager import com.atlassian.jira.issue.Issue import com.atlassian.jira.issue.MutableIssue import java.sql.Timestamp def customFieldManager = ComponentAccessor.getCustomFieldManager() cfParent = customFieldManager.getCustomFieldObjectByName('Launch Date') //Launch Date is the custom field to be moved from the parent parentMyFieldValue = transientVars["issue"].getCustomFieldValue(cfParent) if (DueDate < CurrentDate()) //condition so that due date is not before current date { issue.setDueDate(CurrentDate()) } else { issue.setDueDate(cfParent, new Timestamp((new Date() - 14).time)) }
I am also getting errors related to transientVars.
I am new to groovy so any help would be really useful. Thank you so much!
Hi Thanos!
Thank you so much for your help. I tried to create a post function using your script. Is the following script right? How do i call the issue when i am not testing it? As in i want to avoid using
issue = ComponentManager.getInstance().getIssueManager().getIssueObject("SM_IMSREQUEST-386")
import com.atlassian.jira.ComponentManager import com.atlassian.jira.issue.Issue import com.atlassian.jira.issue.IssueManager import com.atlassian.jira.component.ComponentAccessor issue = ComponentManager.getInstance().getIssueManager().getIssueObject("SM_IMSREQUEST-386") def subtasks = issue.getSubTaskObjects() if (!subtasks) { log.debug "There are no subtasks - do nothing" return } def customFieldManager = ComponentAccessor.getCustomFieldManager() def issueService = ComponentAccessor.issueService def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser() // for all the available subtasks update their due date subtasks.each { subtask -> def parentLaunchDate = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjects(issue).find {it.name == "Initial Launch"}) as Date Calendar cal = Calendar.getInstance() cal.setTime(parentLaunchDate) cal.add(Calendar.DATE, -14) def issueInputParameters = issueService.newIssueInputParameters().setDueDate(cal.getTime().format("d/MMM/yy")) //replace with the format you use def validationResult = issueService.validateUpdate(currentUser, subtask.id, issueInputParameters) assert !validationResult.errorCollection.hasAnyErrors() def issueResult = issueService.update(currentUser, validationResult) log.info "Subtask updated: ${issueResult.issue}" }
i get the output as the subtask key. But when i use it in the post function, the subtasks aren't getting created.
I am not sure if i am doing it right since i am new to this.
Thanks!
Kalpana
Kalpana,
just try to remove
issue = ComponentManager.getInstance().getIssueManager().getIssueObject("SM_IMSREQUEST-386")
The issue variable will be the issue in transition (is bound, you do not have to declare it anywhere in your script).
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Thanos,
I tried adding it as a post function after removing the above line. I witnessed that the sub task was not getting created at all.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Kalpana,
Below is a custom listener, that will listen to issue updated and created events and automatically will update the due date of the updated issue's subtask(s).
import com.atlassian.jira.issue.Issue import com.atlassian.jira.component.ComponentAccessor Issue issue = event.issue def subtasks = issue.getSubTaskObjects() if (!subtasks) { log.debug "There are no subtasks - do nothing" return } def customFieldManager = ComponentAccessor.getCustomFieldManager() def issueService = ComponentAccessor.issueService def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser() // for all the available subtasks update their due date subtasks.each { subtask -> def parentLaunchDate = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjects(issue).find {it.name == "Launch Date"}) as Date Calendar cal = Calendar.getInstance() cal.setTime(parentLaunchDate) cal.add(Calendar.DATE, -14) def issueInputParameters = issueService.newIssueInputParameters().setDueDate(cal.getTime().format("d/MMM/yy")) //replace with the format you use def validationResult = issueService.validateUpdate(currentUser, subtask.id, issueInputParameters) assert !validationResult.errorCollection.hasAnyErrors() def issueResult = issueService.update(currentUser, validationResult) log.info "Subtask updated: ${issueResult.issue}" }
Now, fro the script you posted I can see that you are trying to implement a post function during a workflow transition. If that is the case then you can easily modify the script above to fit in a post function (even thought I think a listener will be a more appropriate solution), please let me know if you need further assistance.
regards
Thanos
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.