Changes the parent ticket field based on the sub-task field

Lakshmi S
Contributor
July 5, 2024

Hi Team,

We are looking for the configuration to update the parent ticket field value based on the sub-task field value using the scriptrunner

The requirement is :

If the main task has "n" number of sub-tasks, and if the "Scheduled Completion Date" of any subtask is changed to a later date(farthest), we should update the main task with that date. 

Ex : If subtasks "Scheduled Completion Date" are sub-task1 = June 23, sub-task2 =July 20, sub-task3 = July 26, etc., and a user changes any of these sub-tasks to August 12, the main ticket should also changes to August 12th.

2 answers

0 votes
John Funk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 6, 2024

Hi Lakshmi,

I do that with a simple Automation Rule:

Screenshot.png

0 votes
Matt Parks
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 5, 2024

You could probably do this through an Automation rule, although I admit to not being super familiar with them, so I'm not sure how the logic would work to get the latest date.

If you wanted to use Scriptrunner, you would still probably want to have an Automation rule that triggers when the Scheduled Completion Date field is updated. The rule would trigger a custom event, but only when the field was updated on the particular subtask issue type (so it wouldn't trigger when the parent's value was updated).

I'm not sure what level of assistance you need with the Scriptrunner script, but something like this would do most of what you want. In this example, I'm using a field called "Target End", but you could change the name of the field you're looking for.

The last section updates the field on the parent, but doesn't generate an IssueUpdated event. It also re-indexes the parent issue so that the new field value will be used when doing any kind of JQL search.

 

import com.atlassian.jira.security.JiraAuthenticationContext;
import com.atlassian.jira.issue.index.IssueIndexingService;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.util.ImportUtils;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.index.IssueIndexingService;
import com.atlassian.jira.event.type.EventDispatchOption;


ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
def issueIndexingService = ComponentAccessor.getComponent(IssueIndexingService);

def targetEndField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName("Target End").first()
Date latestDate = null
def targetEndValue
def issue = event.issue
def parentIssue = issue.getParentObject() as MutableIssue
Collection<Issue> subtasks = parentIssue.getSubTaskObjects()

subtasks.each
{
    targetEndValue = it.getCustomFieldValue(targetEndField) as Date
    if (!latestDate && targetEndValue)
    {
        latestDate = targetEndValue
        return;
    }

    if (targetEndValue > latestDate)
    {
        latestDate = targetEndValue
       
    }
   
}

parentIssue.setCustomFieldValue(targetEndField, latestDate)
ComponentAccessor.getIssueManager().updateIssue(user, parentIssue, EventDispatchOption.DO_NOT_DISPATCH, false)
boolean wasIndexing = ImportUtils.isIndexIssues();
ImportUtils.setIndexIssues(true);
issueIndexingService.reIndex(ComponentAccessor.getIssueManager().getIssueObject(issue.id));
ImportUtils.setIndexIssues(wasIndexing);

Suggest an answer

Log in or Sign up to answer