JMCF Iterating through subtasks

Steven Behnke Community Champion Feb 28, 2015

I've traditionally used ScriptRunner to do these operations but I'm finding it not functional for my specific use-case. Instead, I am trying to use JMCF to iterate through specific subtasks and sum up a field's values.

This is my code, but I get the following error:

<!-- @@Formula: 
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.fields.CustomField;
        IssueManager issueManager = ComponentAccessor.getIssueManager();
        CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
        final CustomField customFieldObject = customFieldManager.getCustomFieldObject("customfield_11811");
        if(customFieldObject == null){
            return null;
        }
        double total = 0;
        if (issue != null) {
            Collection<Issue> subTaskObjects = issue.getSubTaskObjects();
            if(subTaskObjects != null && subTaskObjects.size() != 0){
                for (Issue subTaskObject : subTaskObjects) {
                if (subTaskObject.issueTypeObject.name == "Specific Subtask") {
                    private Object value = customFieldObject.getValue(subTaskObject);
                    total += value;
                    }
                }
                return total;
            }
            else{
                return null;
            }
        }
        return null; 
-->
ERROR [com.innovalog.jmcf.fields.CalculatedNumberField] CalculatedNumberField: error evaluating formula: Parse error at line 17, column 46.  Encountered: =
ERROR [com.innovalog.jmcf.fields.CalculatedNumberField] CalculatedNumberField: error evaluating formula: Sourced file: inline evaluation of: ``   import com.atlassian.jira.component.ComponentAccessor;  import com.atlassian. . . . '' : Typed variable declaration : Final variable, can't re-assign.

I've just been pulling these with JAVAMelody for now. 

I don't encounter these errors in ScriptRunner so I am assuming that the BeanShell library just doesn't support as much JAVA as scriptrunner does. But I don't seem to be able to figure out my issue, so I'm poking around for some help. Thanks in advanced.


My updated code. This is not throwing errors, but also only is returning null. I am not sure if this because customfield11811 is a calculated field of it's own or not.

<!-- @@Formula: 
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.fields.CustomField;
    
        IssueManager issueManager = ComponentAccessor.getIssueManager();
        CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
        CustomField customFieldObject = customFieldManager.getCustomFieldObject("11811");
            
        if(customFieldObject == null){
            return null;
        }
        double total = 0;
        if (issue.equals == null) {
            Collection<Issue> subTaskObjects = issue.getSubTaskObjects();
            if(subTaskObjects = null && subTaskObjects.size() = 0){
                for (Issue subTaskObject : subTaskObjects) {
                if (subTaskObject.issueTypeObject.name.equals("Specific Subtask")) {
                    Object value = customFieldObject.getValue(subTaskObject);
                    total += value;
                    }
                }
                return total;
            }
            else{
                return null;
            }
        }
        return null; -->

2 answers

1 accepted

BeanShell is close to Java, but ScriptRunner uses Groovy, which is a language on its own.

First of all, get rid of the "private" keyword. Then, don't use "==" to compare strings, use ".equals()" instead. Also, I'm not sure the "final" keyword is supported.

Steven Behnke Community Champion Feb 28, 2015

Thanks David, I've been in the BeanShell docs but I'm still picking up the nuances. I seem to have pasted the wrong copy, I was _only_ using "final", not "private" in my test instance. The new version is not erroring out at all! But it's still null. Back to check my logic. New question: Can I calculate a field based on other calculated fields?

getCustomFieldObject("11811") should read getCustomFieldObject("customfield_11811")

In general, you can use the log object to write to the JIRA log file: log.error("some text");

Steven Behnke Community Champion Feb 28, 2015

Thanks for the hints, "you've been helpful" doesn't do it justice. My latest snafu is "Collection<Issue> subTaskObjects.equals(issue.getSubTaskObjects());" It doesn't seem to like the "<" ">" symbols. :p

That's right, BeanShell doesn't support Generics.

Steven Behnke Community Champion Mar 01, 2015

You've been a great help David. Thank you for the excellent add-ons, we regularly use them with customers. I am currently solving this problem with a mix of ScriptRunner and JMCF, but as time allows I'll finish this code and move completely to your add-on, so I can have a single solution.

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

361 views 0 6
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