Script Runner help

Venkat Krishnamoorthy December 19, 2016

 

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
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.
December 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}")
        }
    }
}
Venkat Krishnamoorthy December 20, 2016

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'))) {

....

...

}

Like Ivana Konjicija likes this
Jonny Carter
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.
December 20, 2016

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.

Venkat Krishnamoorthy December 21, 2016

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.

 

Thanos Batagiannis [Adaptavist]
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.
December 21, 2016

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

Venkat Krishnamoorthy December 27, 2016

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

0 votes
Venkat Krishnamoorthy December 21, 2016

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

Jonny Carter
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.
December 21, 2016

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?

Venkat Krishnamoorthy December 27, 2016

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