Fire update Event using scriptrunner

Hi 

I'm trying to write a script that update issue custom field

the script is working and the custom field is indeed updated but there is no indication of the issue changed event in the history

any idea why is that?

thanks

Dar

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.util.IssueChangeHolder
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.event.type.EventDispatchOption;
import com.atlassian.jira.security.JiraAuthenticationContext
IssueManager issueManager = ComponentAccessor.getOSGiComponentInstanceOfType(IssueManager.class);
def cfManager = ComponentAccessor.getCustomFieldManager(); 
Issue issue = issueManager.getIssueObject("TMP-8136");
IssueEvent event;
MutableIssue iss=issue
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
IssueChangeHolder changeHolder = new DefaultIssueChangeHolder();           
def Cf=cfManager.getCustomFieldObject('customfield_10801');    
Double num=5
Cf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(Cf),num ), changeHolder);
ComponentAccessor.getIssueManager().updateIssue(currentUser, iss, EventDispatchOption.ISSUE_UPDATED, false);

1 answer

1 accepted

2 votes
Vasiliy Zverev Community Champion Nov 16, 2016

Note that you update value for iss, but store changes for issue.

I also modified your code, use it if you like this:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.event.type.EventDispatchOption;

IssueManager issueManager = ComponentAccessor.getIssueManager()
MutableIssue issue = issueManager.getIssueObject("TMP-8136");

issue.setCustomFieldValue(
        ComponentAccessor.getCustomFieldManager().getCustomFieldObject('customfield_10801')
        , 5)

ComponentAccessor.getIssueManager().updateIssue(
        ComponentAccessor.getJiraAuthenticationContext().getUser().getDirectoryUser()
        , issue
        , EventDispatchOption.ISSUE_UPDATED
        , false);

Should work fine but the more modern approach is to use IssueService to do an issue update.

Also, the code above is for JIRA 6. On JIRA 7, remove the ".getDirectoryUser()"

Thank you both!! it works great!!

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Sunday in Agility

You asked for it, so we delivered: images on issues have arrived

A picture tells a thousand words. And agility boards have just released their latest feature: cover images on issues – so now your board can tell a story at first glance. Upload attachmen...

932 views 3 12
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you