Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Scriptrunner Listener: setPriority method not working

Diana
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.
June 5, 2023

I have a custom listener script that copies certains fields from a Parent issue to all of it's Child issues via a certain link name. So if the Parent Issue changed CustomFieldA to value "ACB", all the linked Child issues also get updated to have CustomFieldA = "ACB"

The script works fine for custom fields, but I cannot get the system field "Priority" to act the same way. I have tried following examples from here which is the exact same method, but the Child issues will not update.

This is my script, I have scriptrunner version 7.9.0:

EDIT: additional info, i'm using trigger events Issue Created and Issue Updated. I had also attempted to use .setPriorityId, still no results

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.Customfield
import com.atlassian.jira.issue.history.ChangeLogUtils
import com.atlassian.jira.util.ImportUtils

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()
def optionsManager = ComponentAccessor.getComponent(OptionsManager)
def issue = event.issue
def indexService = ComponentAccessor.getComponent(IssueIndexingService.class)
def changeLog = even?.changeLog
def issueChanges = issueManager.getIssueObject(issue.id)
def currentUserObj = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()

if(issue.issueType.name == "Parent"){

    //loop through parent links
    List<IssueLink> allOutIssueLunk = ComponentAccessor.getIssueLinkManager().getInwardLinks(issue.getID())
    for (Iterator<IssueLink> outIterator = allOutIssueLink.iterator(); outIterator.hasNext();){
        IssueLink issueLink = (IssueLink) outIterator.next()

        //define fields needing to copy over
        def customAChanged = false
        def customAField = "Custom A"
        def priorityChanged = false
        def priorityField = "Priority"

        //check to see if the fields were updated, and grab the latest history entry
        def changeHistory = changeHistoryManager.getChangeHistories(issue)
        def history = changeHistory.getAt(changeHistory.size() - 1)
        for (def changeItem : history.getChangeItemBeans()){
            if(changeItem.getField().equals(customAField)){
                customAChanged = true
            } else if (changeItem.getField().equals(priorityField)){
                priorityChanged = true
            }
        }

       
//only update field if the link is a clone
        if (issueLink.getIssueLinkType().getName() == "Cloners"){
        MutableIssue linkedIssue = issueLink.getSourceObject() as MutableIssue

            //only update child issues
            if (linkedIssue.issueType.name == "Child"){

                //update customA, this works
                def customA = customFieldManager.getCustomFieldObjectsByName("Custom A").getAt(0)
                def fieldConfigA = customA.getRelevantConfig(linkedIssue)
                def issueValue = issue.getCustomFieldValue(customA)
                def changeHolder = new defaultIssueChangeHolder()
                customA.updateValue(null, linkedIssue, new ModifiedValue(linkedIssue.getCustomFieldValue(customA), issueValue), changeHolder)
            }

            //update priority << this is not working
            if (priorityChanged){
                def parentPri = issue.getPriority()
                linkedIssue.setPriority(parentPri)
            }

                //update the issue
                issueManager.updateIssue(currentUserObj, linkedIssue, EventDispatchOption.ISSUE_UPDATED, false)
                boolean wasIndexing = ImportUtil.isIndexIssues()
                ImportUtils.setIndexIssues(true)
                indexService.reIndex(linkedIssue)
                ImportUtils.setIndexIssues(wasIndexing)
            }
        }
    }

1 answer

1 accepted

0 votes
Answer accepted
PD Sheehan
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.
June 5, 2023

It might be as simple an issue as a difference in letter case between the internal value of the "changeItem.getField()" and what you see on the screen. I've had no issue using setPriority()

Try to either change your piorityField to "priority" or update the comparison to 

changeItem.getField().equalsIgnoreCase(priorityField)

And add some logs to confirm that "priorityChanged" is set to true when you expect it to.

A couple of other things I noticed:

This seems wrong, 

def changeHolder = new defaultIssueChangeHolder()

Do you mean

def changeHolder = new DefaultIssueChangeHolder()

 

I generally would recommend against using the customField.updateValue() method.

Especially since you already have issueManager.updateIssue() afterwards.  The updateValue will persist the change immediately. So when you do update value and priority didn't change, then updateIssue is doing nothing.

Instead, use issue.setCustomFieldValue(customField, value)

This also removes the need for a changeHolder. The issueManager.updateIssue will take care or if I think.

Diana
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.
June 5, 2023

@PD Sheehan thank you for the quick response

And yes! It worked. It was all because of the lower/upper case issue. This worked:

changeItem.getField().equalsIgnoreCase(priorityField)

And you are correct, I did meant "DefaultIssueChangeHolder()" 

I'll definitely play around changing out customField.updateValue() method in favor for setCustomFieldValue method.

Thanks again!

Suggest an answer

Log in or Sign up to answer