Set Due date to 2 weeks prior to launch date specified

Kalpana Ramesh July 26, 2016

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! 

2 answers

0 votes
Kalpana Ramesh July 29, 2016

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

Thanos Batagiannis _Adaptavist_
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.
July 29, 2016

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).

 

 

Kalpana Ramesh July 31, 2016

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. 

0 votes
Thanos Batagiannis _Adaptavist_
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.
July 28, 2016

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

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events