Script Runner help

 

Hello,

I am trying to update a Script runner example script for our needs and I am having trouble checking a sub-task field value. Please assist. Thanks.

import com.atlassian.jira.component.ComponentAccessor
def issueService = ComponentAccessor.getIssueService()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def subTasks = issue.getSubTaskObjects()
subTasks.each {
    if (it.statusObject.name == "Done" && it.cfValues['Deployed in Environment']*.value.contains('Test')) {
        def issueInputParameters = issueService.newIssueInputParameters()
        issueInputParameters.with {
            // setResolutionId("1") // resolution of "Fixed"
            setComment("Moving subtasks to ToDo from Parent Deployment Request")
            setSkipScreenCheck(true)
        }
        // validate and transition subtask
        def validationResult = issueService.validateTransition(user, it.id, 111, issueInputParameters)
        if (validationResult.isValid()) {
            def issueResult = issueService.transition(user, validationResult)
            if (! issueResult.isValid()) {
                log.warn("Failed to transition subtask ${it.key}, errors: ${issueResult.errorCollection}")
            }
        } else {
            log.warn("Could not transition subtask ${it.key}, errors: ${validationResult.errorCollection}")
        }
    }
}

2 answers

2 votes
Vasiliy Zverev Community Champion Dec 19, 2016

Code seems to be valid. Try to start with

subTask.getCustomFieldValue(deplInEnv)*.value.contains('Test')

I mean that this method returns Object. It is better to manually cust to respective type. What is type for field 

"Deployed in Environment"?

  I refactored your code:

import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.user.ApplicationUser

CustomField deplInEnv = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Deployed in Environment")

IssueService issueService = ComponentAccessor.getIssueService()
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getUser()

for(Issue subTask:issue.getSubTaskObjects()) {
    if ( (      subTask.statusObject.getName() == "Done") 
            &&  subTask.getCustomFieldValue(deplInEnv)*.value.contains('Test')) {
        IssueInputParameters issueInputParameters = issueService.newIssueInputParameters()
        issueInputParameters.with {
// setResolutionId("1") // resolution of "Fixed"
            setComment("Moving subtasks to ToDo from Parent Deployment Request")
            setSkipScreenCheck(true)
        }
// validate and transition subtask
        def validationResult = issueService.validateTransition(user, subTask.getId(), 111, issueInputParameters)
        if (validationResult.isValid()) {
            def issueResult = issueService.transition(user, validationResult)
            if (! issueResult.isValid()) {
                log.warn("Failed to transition subtask ${subTask.key}, errors: ${issueResult.errorCollection}")
            }
        } else {
            log.warn("Could not transition subtask ${subTask.key}, errors: ${validationResult.errorCollection}")
        }
    }
}

Thanks Vasiliy.

"Deployed in Environment" is a Checkbox custom field with multiple options (Dev,Test,Stage,UAT,Prod).

I tried your script and I am still getting "Type Checking" error for that condition. 

BTW, this script is used as a Workflow Transition Post-function script. I was trying to use the syntax from Script Runner documentation (https://scriptrunner.adaptavist.com/latest/jira/recipes/workflow/validators/simple-scripted-validators.html#_validating_cascading_selects)..

 

subTasks.each {
if ((it.statusObject.name == "Done") && (it.cfValues['Deployed in Environment']*.value.contains('Test'))) {

....

...

}

cfValues is only defined for the issue itself. If you're iterating through subtasks, you'll need to use the .getCustomFieldValue method as Vasiliy described.

Sorry, I am kind of new to this. Please bear with me. smile

I am getting the following error for this line - [Static type checking] - Npo such property: value for class:java.lang.Object

    if (subTask.getCustomFieldValue(deplInEnv)*.value.contains('Test')) {
 
What am I missing here? Thanks again.

 

Static Type Checking is helpful but sometimes safe to ignore (as in your case)

Thanks Thanos, that was it. The functionality is working, I just had to ignore the error.

I am trying to use a similar script for Workflow Transition Condition and having the same difficulty. Still couldnt figure out the reason. 

 

import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.issue.fields.CustomField

passesCondition = false

CustomField deplInEnv = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Deployed in Environment")

IssueService issueService = ComponentAccessor.getIssueService()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
for(Issue subTask:issue.getSubTaskObjects()) {
   if (subTask.getCustomFieldValue(deplInEnv)*.value.contains('Dev')) {
      passesCondition = true
   }
}

For the future, it might be good to either update your question or post a new one when you have something like this. Posting a follow-up question as an answer makes for a confusing Q&A thread.

As to your issue, some more detail about the problem would be helpful.

1) Could you log some of the critical variables and pieces in the script, then provide the output from your logs? For example, logging the value of the deplInEnv field

//...
def subtaskFieldValue = subTask.getCustomFieldValue(deplInEnv)*.value
log.debug( "Subtask $subTask.key has deployment environment value $subtaskFieldValue" )
if (subTaskFieldValue.contains('Dev')) { 
//...

The following script from the docs can let you crank up your logging levels so that the contents show up in your atlassian-jira.log file (which you can get the contents of using one of the built-in scripts):

import org.apache.log4j.Level
import org.apache.log4j.Logger

Logger.getLogger("com.onresolve.jira.groovy").setLevel(Level.DEBUG)

2) More detail about the symptom. Is the issue that the condition isn't being applied at all, that it's doing the opposite of what you'd expect, only working sometimes, or what?

Thanks Jonny. This is really my second or third post in Atlassian Answers and I am still trying to get a handle on how to use it right.

I appreciate the time and patience you guys show to help folks like us get up to speed with JIRA customization.

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Sep 25, 2018 in Jira

Atlassian Research Workshop opportunity on Sep. 28th in Austin, TX

We're looking for participants for a workshop at Atlassian! We need Jira admins who have interesting custom workflows, issue views, or boards. Think you have a story to sha...

456 views 7 5
Join discussion

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