Set Due date to 2 weeks prior to launch date specified

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

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

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

 

 

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. 

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Posted Wednesday in Jira

Join our webinar: How 1B+ feature flag events helped us build the new Jira

Every time you release software, there's a bit of risk – that there's a bug, that something breaks, or that the feature doesn't resonate with customers. Feature flagging helps make high stakes s...

91 views 0 1
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you