Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to get a list of modified fields on ScriptRunner on IssueUpdated event?

Arthur Accioly April 16, 2016

I created a Script Listener for the IssueUpdated event and I'm trying to get a list of the changed fields. For some reason the method getModifiedFields() is coming empty. Somebody knows why?

 

import com.atlassian.jira.issue.managers.DefaultIssueManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.ModifiedValue

log.setLevel(org.apache.log4j.Level.DEBUG)

Issue mainIssue = event.issue

MutableIssue mutableIssue = (MutableIssue)mainIssue
def modFields = mutableIssue.getModifiedFields()
log.debug("Modified fields count: "+modFields.count) // null
log.debug("Modified fields: "+modFields.toString()) //  [:]
log.debug("Original Ticket: "+mainIssue.key) // EPS-39

1 answer

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Arthur Accioly April 16, 2016

I could find the answer to my question turning google upside down. Now I'm stuck in the part of the label below. Any help is very welcomed...

 

import com.atlassian.jira.issue.managers.DefaultIssueManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.ModifiedValue
import org.ofbiz.core.entity.GenericValue
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.bc.issue.DefaultIssueService
log.setLevel(org.apache.log4j.Level.DEBUG)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
Issue mainIssue = event.issue
// Get Custom Field Object "Original Ticket"
def originalTicket = customFieldManager.getCustomFieldObjectByName("Original Ticket")
// Get Value of the Custom Field Object "Original Ticket"
String targetTicketKey = mainIssue.getCustomFieldValue(originalTicket).toString()
// Get Original Ticket Object based on the custom field value
def targetIssue = ComponentAccessor.getIssueManager().getIssueObject(targetTicketKey)
// Get list of modified values in the original ticket to update target ticket
List<GenericValue> changeItemsList = event.getChangeLog().getRelated("ChildChangeItem")
Iterator<GenericValue> changeItemListIterator = changeItemsList.iterator()
// Objects related to what changed
Object oldValue
Object newValue
// Variables used to change the value of the target ticket
def userManager = ComponentAccessor.getUserManager()
def auser = userManager.getUserByKey("Sync User")
def issueManager = ComponentAccessor.getIssueManager()
CustomField custom
// Loop for all the changed fields
while (changeItemListIterator.hasNext()) {
    GenericValue changeItem = (GenericValue)changeItemListIterator.next()
    String currentFieldName = changeItem.get("field").toString()
    log.debug("Current field: "+currentFieldName)
    oldValue = changeItem.get("oldstring")
    newValue = changeItem.get("newstring")
    if (oldValue != null && newValue != null){
        log.debug("Field changed from: "+oldValue+" to "+newValue)
        switch (currentFieldName){
            case "summary": 
              log.debug("Found switch: Summary")
              targetIssue.setSummary(newValue.toString())
            break
            case "description": 
              log.debug("Found switch: Description")
              targetIssue.setDescription(newValue.toString())
            break
            case "Affected Version(s)": 
              log.debug("Found switch: Affected Version(s)")
              // This is a label field. I'm stuck here and I don't know how to manipulate the value.
              // Labels are a set type, studing more about it.
              custom = customFieldManager.getCustomFieldObjectByName("Affected Version(s)")
              targetIssue.setCustomFieldValue(custom,newValue)
            break
            default: log.debug("Not found: "+currentFieldName)
        }
    }
    // Update my target Issue (in another project) that I'm trying to synchronize with the main issue.
    issueManager.updateIssue(auser, targetIssue,  com.atlassian.jira.event.type.EventDispatchOption.DO_NOT_DISPATCH, false)    
}
Arthur Accioly April 17, 2016

This is giving me the following error:

java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Set
Arthur Accioly April 18, 2016

Looks like this code can make it work, but now I'm having the "Labels" fields from the target issue receiving my "Affected Version(s)" value. Somebody can help me to find a way of correlating my custom field "Affected Version(s)" in my target issue, instead of the label field?

def labelManager = ComponentManager.getComponentInstanceOfType(LabelManager.class)
def origin = customFieldManager.getCustomFieldObjectByName("Affected Version(s)") 
def labelSet = mainIssue.getCustomFieldValue(origin).toString().tokenize(", ")
labelManager.setLabels(auser,targetIssue.id,labelSet.toSet(),false,false)
TAGS
AUG Leaders

Atlassian Community Events