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

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

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

Set epic status to done in workflow post function with scriptrunner

I want to set the epic status to Done when an epic arrives in the final state of our workflow. The idea is to add a script on a post function on our 'Conclude' transition. So far I have come up with this:

 

import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.customfields.manager.OptionsManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.fields.config.FieldConfig;
import com.atlassian.jira.issue.customfields.option.Options;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.Issue; import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.event.type.*;
import com.atlassian.crowd.embedded.api.User;
 
ComponentManager componentManager = ComponentManager.getInstance();
IssueManager issueManager = componentManager.getIssueManager(); CustomFieldManager customFieldManager = componentManager.getCustomFieldManager();
OptionsManager optionsManager = componentAccessor.getOptionsManager();
User user = componentManager.getJiraAuthenticationContext().getLoggedInUser();
 
// This is to be able to test in Script Console
issue = issueManager.getIssueObject("SCRSAN-22");
 
if (issue.issueType.name == "Epic")
{
    CustomField customField = customFieldManager.getCustomFieldObjectByName("Epic Status");
    FieldConfig fieldConfig = customField.getRelevantConfig(issue);
    Options options = optionsManager.getOptions(fieldConfig);
    MutableIssue issueToUpdate = (MutableIssue) issue;
    return options;
 
    //If I ever get to set the Epic Status, write to issue ;)
    //issueToUpdate.setCustomFieldValue(customField, "Done");
    //issueManager.updateIssue(user, issueToUpdate, EventDispatchOption.ISSUE_UPDATED, false);
}

 

 

This is not working yet and I am wondering if I am on the right track or not. Do I really need to do all the imports and find the right Option to set to the customfield Epic Status. Or can it be done in a simpler way?

6 answers

1 accepted

1 vote
Answer accepted

I got this working with the following

import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.customfields.manager.OptionsManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.fields.config.FieldConfig;
import com.atlassian.jira.issue.customfields.option.Options;
import com.atlassian.jira.issue.customfields.option.Option;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.event.type.*;
import com.atlassian.crowd.embedded.api.User;
ComponentManager componentManager = ComponentManager.getInstance();
IssueManager issueManager = componentManager.getIssueManager(); CustomFieldManager customFieldManager = componentManager.getCustomFieldManager();
OptionsManager optionsManager = ComponentManager.getComponentInstanceOfType(OptionsManager.class);
User user = componentManager.getJiraAuthenticationContext().getLoggedInUser();
if (issue.issueType.name == "Epic")
{
    CustomField epicStatus = customFieldManager.getCustomFieldObjectByName("Epic Status");
    FieldConfig epicStatusFieldConfig = epicStatus.getRelevantConfig(issue);
    Options epicStatusOptions = optionsManager.getOptions(epicStatusFieldConfig);
    Option epicStatusDoneOption = epicStatusOptions.getOptionForValue("Done", null);
    MutableIssue issueToUpdate = (MutableIssue) issue;
    issueToUpdate.setCustomFieldValue(epicStatus, epicStatusDoneOption);
    //The updateIssue is actually not needed
    //issueManager.updateIssue(user, issueToUpdate, EventDispatchOption.ISSUE_UPDATED, false);
}

easy peasy!

I was unable to get the example working (most likely due to newer versions of the tools).

I found an alternative solution on the Adaptavist pages that is much shorter and that works like a charm:

 

import com.atlassian.jira.component.ComponentAccessor
def cfSelect = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Favourite Fruit")
def cfConfig = cfSelect.getRelevantConfig(issue)
def value = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find {
it.toString() == 'Lemons'
}
issue.setCustomFieldValue(cfSelect, value)

 

Replace "Favourite Fruit" with the actual field name in JIRA.

Replace 'Lemons' with the actual custom field select value.

Like Vladislav likes this

Thanks Lars!

Everybody please note that on adaptavist docs there is a typo in the code 

https://docs.adaptavist.com/sfj/set-a-select-list-custom-field-value-11468840.html

Don't forget to fix it as mentioned by Lars - then code works like a charm ;)

code-typo.png

Can you use the {code} macro so that's readable? The hard-coded issue ID stands out as a problem. > Do I really need to do all the imports and find the right Option to set to the customfield Epic Status Pretty much, yes.

OK, I updated to use the code macro. OK, good to know I am on the right path (with all my imports and all). Please excuse me if I start asking rookie questions from here, as that's basically what I am in this SciptRunner/JIRA API stuff. The hard-coded issue ID was in there to be able to test/debug in the Script Console. That way I can see what is happening and don't have to go through workflow modifications. Is there a 'better' way to write/debug these scripts? Anyway, it seem I to need access the optionsManager to find the "Done" option. Seem to have found the following in a post by you from 2012. Will see if I can get that in there and find the right option to set to the customfield. OptionsManager optionsManager = ComponentManager.getComponentInstanceOfType(OptionsManager.class)

Yes, if it's a select list you are on the right track there.

Thanks, I got it working.

Hi Peter,

I had exactly this issue at Twitter. I've detailed how we addressed this in Understanding Cycle Time of JIRA Epics.

May be of use in your scenario.

Regards,
Nicholas Muldoon
Arijea 

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Marketplace Apps & Integrations

Bitbucket Smart Commits vs. Genius Commits - What's the difference?

If you already heard about Smart Commits in Bitbucket, know that you just stumbled upon something even better (and smarter!): Genius Commits by Better DevOps Automation for Jira Data Center (+ Server...

130 views 0 2
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