Script listener: Copy specific field value from parent to each sub task

Deleted user August 22, 2017

Hi

I would like to have a script listener which updates each sub-task if a specific field on their parent task has been updated.

Here's what I have so far.

import com.atlassian.jira.component.ComponentAccessor
import org.ofbiz.core.entity.GenericValue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.DefaultUserManager

import org.apache.log4j.Level
log.setLevel(Level.DEBUG)
log.debug("--------------------This is to test Behaviours Logging----------------")

def issue = event.issue
def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def subTasksList = issue.getSubTaskObjects()
def type = issue.issueType.name

def changeHolder = new DefaultIssueChangeHolder()

def userManager = ComponentAccessor.getUserManager()
def tgtField = customFieldManager.getCustomFieldObjectByName("Projektleiter")

if(type == "Projekt")
{
    // Get list of modified values
    List<GenericValue> changeItemsList = event.getChangeLog().getRelated("ChildChangeItem")
    Iterator<GenericValue> changeItemListIterator = changeItemsList.iterator()
    
    String oldValue
    String newValue
    
    while (changeItemListIterator.hasNext())
    {
        GenericValue changeItem = (GenericValue)changeItemListIterator.next()
        String currentFieldName = changeItem.get("field").toString()
        
        if(currentFieldName == "Projektleiter")
        {
            oldValue = changeItem.get("oldstring")
            newValue = changeItem.get("newstring")
            
            if (oldValue != newValue)
            {
                log.debug("Field changed from: "+oldValue+" to "+newValue)
                
                ApplicationUser NewProjectLeader = userManager.getUserByName(newValue)
                
                for(subTask in subTasksList)
                {
                    tgtField.updateValue(null, subTask, new ModifiedValue(issue.getCustomFieldValue(tgtField), NewProjectLeader),changeHolder)
                    
                    log.debug(subTask)
                }
            }
        }  
    }
}

I'm able to say if the specific field actually has been updated and then also get the new value. I just can't get it to update the field on each sub-task.

Thanks for any help.

1 answer

0 votes
Joshua Yamdogo @ 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.
August 22, 2017

I would think that that the line where you're calling updatevalue() should use the old value of the subtask, not of the original issue. Perhaps try subtask.getCustomFieldValue(tgtField).

tgtField.updateValue(null, subTask, new ModifiedValue(subtask.getCustomFieldValue(tgtField), NewProjectLeader),changeHolder)
Deleted user August 22, 2017

Hi Joshua

Thanks for your help so far. Unfortunately, it does not work. It just empties the field on each sub-task.

Joshua Yamdogo @ 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.
August 22, 2017

What type of custom field is Projektleiter? Is it a User Picker field?

Deleted user August 22, 2017

Yes, exactly. Single User Picker.

Joshua Yamdogo @ 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.
August 22, 2017

Marius,

I believe your problem with the script is coming from this line:

ApplicationUser NewProjectLeader = userManager.getUserByName(newValue)

The problem with doing this is that newValue is NOT a username. newValue is their display name. You can't call getUserByName using someone's display name and there is no method for doing so. If you add log.debug(NewProjectLeader), you'll see that it is null.

Try the following script instead:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import org.apache.log4j.Level
import org.ofbiz.core.entity.GenericValue

log.setLevel(Level.DEBUG)
log.debug("--------------------This is to test Behaviours Logging----------------")

def issue = event.issue
def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def subTasksList = issue.getSubTaskObjects()
def type = issue.issueType.name

def changeHolder = new DefaultIssueChangeHolder()

def userManager = ComponentAccessor.getUserManager()
def tgtField = customFieldManager.getCustomFieldObjectByName("Projektleiter")

if(type == "Projekt")
{
// Get list of modified values
List<GenericValue> changeItemsList = event.getChangeLog().getRelated("ChildChangeItem")
Iterator<GenericValue> changeItemListIterator = changeItemsList.iterator()

String oldValue
String newValue

while (changeItemListIterator.hasNext())
{
GenericValue changeItem = (GenericValue)changeItemListIterator.next()
String currentFieldName = changeItem.get("field").toString()

if(currentFieldName == "Projektleiter")
{
oldValue = changeItem.get("oldstring")
newValue = changeItem.get("newstring")

if (oldValue != newValue)
{
log.debug("Field changed from: " + oldValue + " to " + newValue)

def NewProjectLeader = issue.getCustomFieldValue(tgtField)

for(subTask in subTasksList)
{
tgtField.updateValue(null, subTask, new ModifiedValue(subTask.getCustomFieldValue(tgtField), NewProjectLeader),changeHolder)

log.debug(subTask)
}
}
}
}
}

This script worked me in a listener to update the subtask custom field value whenever the parent gets updated.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events