It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

Marius Vollrath Aug 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 Community Leader Aug 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)
Marius Vollrath Aug 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 Community Leader Aug 22, 2017 • edited

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

Marius Vollrath Aug 22, 2017

Yes, exactly. Single User Picker.

Joshua Yamdogo Community Leader Aug 22, 2017 • edited

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
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Marketplace Apps & Integrations

Webinar invitation: Generate custom Jira Cloud PDF reports and exports

Jira may be a staple part of your workday, but your colleagues, customers, business partners or an auditor might need information presented in a different format, that is easy to access and read. And...

42 views 0 1
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you