How to add custom imports to 'Create sub-task'?

Hi,

I'm using the 'Create sub-task' option in this exceptional tool, but I have a problem referring to my constants 'CustomFields' class.

When this code is triggered:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import org.apache.log4j.*
import groovy.util.logging.*
import static com.ubs.sr.constants.CustomFields.*

def Logger log = Logger.getInstance(getClass())
log.level = Level.debug
log.info "Workflow - In Progress - Create QA sub-task"
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField cfQaRequiredYN = customFieldManager.getCustomFieldObject(CFQAREQUIREDYN)
log.info "QA Required = ${issue.getCustomFieldValue(cfQaRequiredYN)}"
issue.issueTypeObject.name == 'Story' && ((issue.getCustomFieldValue(cfQaRequiredYN).toString() ?: "").equals("Yes"))

it logs these two errors in the log file:

cript4.groovy: 6: unable to resolve class com.ubs.sr.constants.CustomFields
 @ line 6, column 1.
   import static com.ubs.sr.constants.CustomFields.*
   ^
Script4.groovy: 6: unable to resolve class com.ubs.sr.constants.CustomFields
 @ line 6, column 1.
   import static com.ubs.sr.constants.CustomFields.*
   ^
2 errors

The answer to my earlier 'ScriptRunner Primer' question works perfectly for scripts, but the Create Sub-task 'script-lit' won't work with this.

Being able to refer to the custom fields by constants makes the deployment to another instance easier as its only a single place to update. Is this possible with the 'Create sub-task' function?

Kind regards,

Richard 

2 answers

1 accepted

Answering my own last question: - adding '(CreateSubTask.Field_SubTask_Action) : 1' does the trick am

createSubTask.doScript([
issue : issue,
(CreateSubTask.FIELD_TARGET_ISSUE_TYPE) : constantsManager.getSubTaskIssueTypeObjects().find { it.name == ITYPE_QASUBTASK }.id.toString(),
(CreateSubTask.FIELD_SUBTASK_SUMMARY) : "New QA Sub-task - please update",
(CreateSubTask.FIELD_SUBTASK_ACTION) : 1 // Step ID for Open
])
1 vote

Unfortunately that doesn't work - this is https://jamieechlin.atlassian.net/browse/GRV-443. The reasons why are a bit dull, but when you point to a file it's executed by a GroovyScriptEngine, when it's an inline script it's the groovy javax.script.ScriptEngine, and the two have different capabilities. This is something I need to get to.

You can work around it by using a custom script post-function, and just calling the CreateSubTask built-in script, like this. It's not totally satisfactory but it works, and it's better than duplicating your constants around.

package ...

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.CreateSubTask
import com.ubs.Constants

// all your condition code here...
def conditionPasses = true

def constantsManager = ComponentAccessor.getConstantsManager()
if (conditionPasses) {
    def createSubTask = new CreateSubTask()
    createSubTask.doScript([
        issue                                  : issue,
        (CreateSubTask.FIELD_TARGET_ISSUE_TYPE): constantsManager.getSubTaskIssueTypeObjects().find {
            it.name == "Sub-task"
        }.id.toString(),
        (CreateSubTask.FIELD_SUBTASK_SUMMARY)  : "some summary",
    ])
}

Hi Jamie Many thanks for the prompt reply. I'll implement that solution and keep the information for future reference. Cheers Richard

Thanks - I was able to implement this today. But I now have a related question - which parameters do I set ask it to reopen any existing matching sub-tasks rather than create new ones?

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...

77 views 0 5
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