Can't change fix version in post-function script

I need to add fix version for the linked issue. But script I've wrote doesn't take effect.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.user.ApplicationUser

String processBlockingIssue(MutableIssue origin, Issue issue) {

   ApplicationUser user = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()
   issue.fixVersions.addAll(origin.fixVersions)
   ComponentAccessor.getIssueManager().updateIssue(user, issue, EventDispatchOption.ISSUE_ASSIGNED, true);
   return origin.fixVersions.join(",");
}

MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject("LIC-197");
MutableIssue myIssue = issue
List<IssueLink> links = ComponentAccessor.getIssueLinkManager().getOutwardLinks(myIssue.id)
String str = "";
for (IssueLink link : links) {
   if ("Blocks".equalsIgnoreCase(link.issueLinkType.name)) {
      str += processBlockingIssue(myIssue, link.destinationObject)
   }
}
str;

I get result "0,5" after calling script in script console. That is only version that alredy assign to issue LIC-197. I have issue LIC-196 that blocks by LIC-197.
Issue LIC-196 and fixVersion define correctly. But issue is not updated.

1 answer

when updating most native fields like fixVersion I always have to do a

issue.store();

and based on the way you are creating your issue, you should also do a reindex at the end of the post function.

Change processBlockingIssue to this 

ApplicationUser user = ComponentAccessor.jiraAuthenticationContext.user
issue.setFixVersions(origin.getFixVersions())
issue.store();

But it doesn't work.

start putting in some log statements and make sure you are updating what you think you are updating.  That your loop actually finding LIC-196 and trying to update it with what ever is in LIC-197 (your Origin).

you could also try casing issue as a MutableIssue.  and see if that helps.

As I also mentioned you probably need to reindex it.

 

 

script was change to 

String processBlockingIssue(MutableIssue origin, MutableIssue issue) {
	Project project = issue.getProjectObject()
	Version version = ComponentAccessor.getVersionManager().getVersion(project.getId(), "0.5")
	def res = "\n Updating issue ${issue}."
	issue.setFixVersions([version])
	issue.description = "changed description"
	issue.store();
	boolean wasIndexing = ImportUtils.isIndexIssues();
	ImportUtils.setIndexIssues(true);
	ComponentAccessor.getIssueIndexManager().reIndex(ComponentAccessor.getIssueManager().getIssueObject(issue.getId()));
	ImportUtils.setIndexIssues(wasIndexing);

	res += " Set fix version = [" + issue.fixVersions.join(",") +"]. Saved fix versions = ["+ ComponentAccessor.getIssueManager().getIssueObject(issue.getId()).fixVersions.join(",")+"]"
	return res;
}

After execution in console I have result 

pdating issue LIC-196. Set fix version = [0.5]. Saved fix versions = []

But description have been changed what is wrong? May be there is other way to save fixVersions?

Yes there is another way.. There are actually a couple of ways.  here is some code I use that works... and BTW, you version 0.5  .. it wouldn't happen to be archived would it?

 

Also looking at some older code, this should have worked for you.

issueManager.updateIssue(userManager.getUserObject('automation'), issue, EventDispatchOption.DO_NOT_DISPATCH, false);

 

 

public void setFixVersions4Issue(Long versionID, MutableIssue issue) {
    IssueChangeHolder changeHolder = new DefaultIssueChangeHolder();
    OrderableField field = ComponentAccessor.getFieldManager().getOrderableField("fixVersions");

    Collection<Version> Col_Versions = issue.getFixVersions();
    Collection<Version> archVers = VersionUnArchive(Col_Versions);
    Col_Versions.clear();
    if (versionID == null) {
        issue.setFixVersions(null);
    } else {
        Version tempVersion = versionManager.getVersion(versionID);
        Col_Versions.add(tempVersion);
        issue.setFixVersions(Col_Versions);
    }
    field.updateValue(null, issue, new ModifiedValue(issue.getFixVersions(), Col_Versions), changeHolder);
    VersionReArchive(archVers);
	indexManager.reIndex(issue);
}

public Collection<Version> VersionUnArchive(Collection<Version> inVersions) {
    Logger thislog = Logger.getLogger("VersionUnArchive");
    thislog.setLevel(Level.ERROR);
    thislog.debug("Starting VersionArchive");
    Collection<Version> archVers = new ArrayList<>();
    for (Version version : inVersions) {
        if (version.isArchived()) {
            archVers.add(version);
            version.setArchived(false);
        }
    }
    return archVers;
}

public void VersionReArchive(Collection<Version> inVersions) {
    Logger thislog = Logger.getLogger("VersionReArchive");
    thislog.setLevel(Level.ERROR);
    thislog.debug("Starting VersionArchive");
    for (Version version : inVersions) {
        version.setArchived(true);
    }
}

I have found error. All I need it's to change one line. 

ComponentAccessor.getIssueManager().updateIssue(user.directoryUser, (MutableIssue) issue, EventDispatchOption.ISSUE_UPDATED, Boolean.FALSE)

 EventDispatchOption.ISSUE_ASSIGNED must be replaced by EventDispatchOption.ISSUE_UPDATED

Suggest an answer

Log in or Join to answer
Community showcase
Teodora [Botron]
Published Thursday in Marketplace Apps

Jira Inferno: The Nine Circles of Jira Administration Hell

If you spend enough time as a Jira admin - whether you are managing a single, mid-sized instance, a large enterprise one or juggling multiple instances at once - you will eventually find yourself in ...

590 views 2 15
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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot