JMCF Iterating through subtasks

Steven F Behnke
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.
February 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

1 vote
Answer accepted
David _old account_
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.
February 28, 2015

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 F Behnke
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.
February 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?

1 vote
David _old account_
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.
February 28, 2015

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 F Behnke
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.
February 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

David _old account_
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.
March 1, 2015

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

Steven F Behnke
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.
March 1, 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
TAGS
AUG Leaders

Atlassian Community Events